title: fsockopen() timeout sometimes not working
body: <p>Sending some data points constantly (aiming for once a second), using an infinite loop of POST requests in PHP. The data is highly time-dependent. If there's a timeout, I don't care about the lost data, I just want to move on to the next datapoint quickly.</p>
  
  <p>I originally used <code>file_get_contents()</code> (with a timeout value of 1) to do this POST request, but I was having some issues where it should have been timing out and wasn't. I read a lot of questions about this and the general consensus was that the timeout on <code>file_get_contents()</code> wasn't being triggered because it's a <em>read</em> timeout, rather than a <em>connect</em> timeout. </p>
  
  <p>I also read that sockets have both kinds of timeout, so I changed the code to use <code>fsockopen()</code> instead. A snippet is included below. </p>
  
  <pre><code>    // waaaay up at the top of the file
      ini_set("default_socket_timeout", 1); // set default socket connect timeout
      //...
      // inside the loop
      echo("4...");
      $url = 'othersite.ie';
      $fp = fsockopen($url, 80, $errno, $errstr, 1); // connect timeout of 1
      if(!$fp)
      {
          echo($errno .": ". $errstr);
      }
      else
      {
          echo("5...");
          stream_set_timeout($fp, 1, 0); // read timeout of 1
  
          $content = http_build_query($data); // array assembled before this snippet               
          fwrite($fp, "POST /some/address/at/url HTTP/1.1\r\n");
          fwrite($fp, "Host: www.examplesite.ie\r\n");
          fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
          fwrite($fp, "Content-Length: ".strlen($content)."\r\n");
          fwrite($fp, "Connection: close\r\n");
          fwrite($fp, "\r\n");
  
          fwrite($fp, $content);
  
          while(!feof($fp))
          {
              $result = $result . fgets($fp, 1024);
          }
          echo("6...");
  
          if (empty($result))
          {
              echo("No result");
          }
      }
  </code></pre>
  
  <p>This didn't seem to help, though. I added some <code>echo</code> statements so if I was watching the output, I could see where the delay happened, and it has been invariably between "4..." and "5...". Each delay is about ten seconds, and they don't seem to have any particular order to them. Most of the time (more than 99%) the connection works, the read works, and I get my data to the server. But it's important to at least try to eliminate those 10-second hangs. Any help appreciated.</p>