title: Thousands of TIME_WAIT despite SO_LINGER, shutdown etc
body: <p>I'm working on a site that connects to many URLs at once, (we are hoping to get to ~600 per minute) and no matter what I try, there are always thousands of TIME_WAIT still open. I understand these are vital to TCP connections, but they are using all the available ports. PHP doesn't have a SO_REUSEPORT, and SO_REUSEADDR doesn't work with remote connections. Here is the beginning of the code:</p>
  
  <pre><code>$s = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_set_option($s, SOL_SOCKET, SO_LINGER,array('l_linger'=&gt;0, 'l_onoff'=&gt;0)); //I have tried l_onoff=1 
  socket_set_option($s, SOL_SOCKET, SO_RCVTIMEO,array('sec'=&gt;0,'usec'=&gt;500000));
  socket_set_option($s, SOL_SOCKET, SO_SNDTIMEO,array('sec'=&gt;0,'usec'=&gt;500000));
  socket_set_option($s, SOL_SOCKET, SO_KEEPALIVE,0);
  socket_set_option($s, SOL_SOCKET, SO_REUSEADDR,1);
  socket_set_nonblock($s);
  socket_bind($s,$ip,0);
  socket_connect($s,$host,$port);
  </code></pre>
  
  <p>The $s goes into an array contains all the pending writes, after a write we call socket_shutdown($s,1); to close writing on the socket. Then after a read we:</p>
  
  <pre><code>socket_shutdown($s,2); socket_close($s);
  </code></pre>
  
  <p>All reading and writing is done in a while loop, the loop has a max of 12 concurrent connections, if that is not hit, then it moves on and proceeds to add another URL to the array. Every loop calls socket_select with a timeout of 0.</p>
  
  <p>Does anyone have any suggestions? I would like to increase the speed as well as reduce the TIME_WAIT's that appear in netstat.</p>
  
  <p>Thanks,
  James Hartig</p>