title: Process socket data with a leading length value
body: <p>This is a <a href="https://stackoverflow.com/questions/9742449/sending-sockets-data-with-a-leading-length-value">follow-up question</a> about how to process prefixed messages received from a network socket. What I try to do is:</p>
  
  <ol>
  <li>Read the first 4 bytes (which is the $prefix and represents the length of the message)</li>
  <li>Check if $prefix has really the size of 4 bytes and if it's an integer</li>
  <li>Read the complete $message using the length from $prefix</li>
  <li>Check if message has really the size of $prefix bytes</li>
  </ol>
  
  <p>So far I've the following two lines of code:</p>
  
  <pre><code>    $prefix = socket_read($socket, 4, PHP_BINARY_READ); //No 1.
      //No 2: how to do the checks?
      $message = socket_read($socket, $prefix, PHP_BINARY_READ); //No 3.
      //No 4: how to do the checks?
  </code></pre>
  
  <p>How can I do the mentioned checks?</p>
  
  <p>A little side note: all data sent through the network socket connection is in UTF8, little-endian</p>