title: PHP fsockopen() works once but fails on subsequent tries
body: <p>I'm trying to poll a server at intervals using a PHP socket connection. However, when I run the script I get the correct behaviour the first time, but then the second time I get the following error:</p>
  
  <pre><code>fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known
  </code></pre>
  
  <p>When I run the script a third time I get the following error:</p>
  
  <pre><code>fsockopen(): unable to connect to :0 (Failed to parse address "")
  </code></pre>
  
  <p>That error repeats every subsequent attempt.
  However, if I wait about a minute and retry then everything works. It seems to me like I'm having a problem with the socket connection not closing and then being refused when I try to create it again.</p>
  
  <p>The class that handles the socket communication is:</p>
  
  <pre><code>class FsSocket
  {   
  private $password = "XXXXXXXXXX";
  private $port = "8030";
  private $host = "hostname";
  
  function event_socket_create( $host=null, $port=null, $password=null ) {
      if ( $host == null ) {
          $host = $this-&gt;host;
      }
      if ( $port == null ) {
          $port = $this-&gt;port;
      }
      if ( $password == null ) {
          $password = $this-&gt;password;
      }
  
      error_log( $host.":".$port );
      $fp = fsockopen($host, $port, $errno, $errdesc, 2) or die("Connection to $host failed");
      socket_set_blocking($fp,false);
  
      if ($fp) {
          while (!feof($fp)) {
              $buffer = fgets($fp, 1024);
              usleep(100); //allow time for reponse
              if (trim($buffer) == "Content-Type: auth/request") {
              fputs($fp, "auth $password\n\n");
              break;
              }
          }
          return $fp;
      }
      else {
          return false;
      }           
  }
  
  
  function event_socket_request($fp, $cmd) {
  
      if ($fp) {    
          fputs($fp, $cmd."\n\n");    
          usleep(100); //allow time for reponse
  
          $response = "";
          $i = 0;
          $contentlength = 0;
          while (!feof($fp)) {
              $buffer = fgets($fp, 4096);
              if ($contentlength &gt; 0) {
              $response .= $buffer;
              }
  
              if ($contentlength == 0) { //if contentlenght is already don't process again
                  if (strlen(trim($buffer)) &gt; 0) { //run only if buffer has content
                      $temparray = explode(":", trim($buffer));
                      if ($temparray[0] == "Content-Length") {
                          $contentlength = trim($temparray[1]);
                      }
                  }
              }
  
              usleep(100); //allow time for reponse
  
              //optional because of script timeout //don't let while loop become endless
              if ($i &gt; 10000) { break; } 
  
              if ($contentlength &gt; 0) { //is contentlength set
                  //stop reading if all content has been read.
                  if (strlen($response) &gt;= $contentlength) {  
                  break;
                  }
              }
              $i++;
          }
  
          return $response;
      }
      else {
      echo "no handle";
      }
  }
  }
  </code></pre>
  
  <p>The code that calls the socket class is as follows:</p>
  
  <pre><code>$socket = new FsSocket();
  $fp = $socket-&gt;event_socket_create();
  
  $cmd = "sofia_contact $to-&gt;user@$to-&gt;domain";
  $response = $socket-&gt;event_socket_request($fp, $cmd);
  
  fclose($fp);
  unset($socket);
  </code></pre>
  
  <p>I haven't implemented the loop bit yet. I'm trying to get this working by manually refreshing the page first.</p>