Simple PHP TCP Server

With PHP, creating a TCP server takes only a few lines of code. Check out this code that creates a server which responds with the current time.

$socket = stream_socket_server("tcp://", $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)\n";
  die('Could not create socket');

while (true) {
  while ($conn = stream_socket_accept($socket, -1, $peername)) {
    fwrite($conn, 'The local time is ' . date('n/j/Y g:i a') . "\n");
    echo "Connection received from: $peername\n";