title: Correct usage of socket_select()
body: <p>What is the correct way to use socket_select within PHP to send and receive data?</p>
  
  <p>I have a connection to the <em>server</em> that allows for both TCP &amp; UDP packet connections, I am utilizing both. Within these connections I'm both sending and receiving packets on the same port, but the TCP packet will be sent on one port (<code>29999</code>) and UDP will be sent on another port (<code>30000</code>). The transmission type will be that of <code>AF_INET</code>. The IP address will be loopback <code>127.0.0.1</code>.</p>
  
  <p>I have many questions on how to create a socket connection within this scenario. For example, is it better to use <a href="http://php.net/manual/en/function.socket-create.php" rel="nofollow noreferrer">socket_create_pair</a> to make the connection, or use just <a href="http://php.net/manual/en/function.socket-create-pair.php" rel="nofollow noreferrer">socket_create</a> followed by <a href="http://php.net/manual/en/function.socket-connect.php" rel="nofollow noreferrer">socket_connect</a>, and then implement <a href="http://php.net/manual/en/function.socket-select.php" rel="nofollow noreferrer">socket_select</a>?</p>
  
  <p>There is a chance that no data will be sent from the server to the client, and it is up to the client to maintain the connection. This will be done by utilizing the time out function within the socket_select call. Should no data be sent within the time limit, the socket_select function will break and a keep alive packet can then be sent. The following script is of the client.</p>
  
  <pre><code>// Create
  $TCP = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  $UDP = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  
  // Misc
  $isAlive = TRUE;
  $UDPPort = 30000;
  define('ISP_ISI', 1);
  
  // Connect
  socket_connect($TCP, '127.0.0.1', 29999);
  socket_connect($UDP, '127.0.0.1', $UDPPort);
  
  // Construct Parameters
  $recv = array($TCP, $UDP);
  $null = NULL;
  
  // Make The Packet to Send.
  $packet = pack('CCCxSSxCSa16a16', 44, ISP_ISI, 1, $UDPPort, 0, '!', 0, 'AdminPass', 'SocketSelect');
  
  // Send ISI (InSim Init) Packet
  socket_write($TCP, $packet);
  
  /* Main Program Loop */
  while ($isAlive == TRUE)
  {
      // Socket Select
      $sock = socket_select($recv, $null, $null, 5);
  
      // Check Status
      if ($sock === FALSE)
          $isAlive = FALSE; # Error
      else if ($sock &gt; 0)
          # How does one check to find what socket changed?
      else
          # Something else happed, don't know what as it's not in the documentation, Could this be our timeout getting tripped?
  }
  </code></pre>