Write a packet to a socket, preferably using socket_write()
===

The script I am attempting to write emulates a Minecraft server interface, but without any functionality. The problem I am having is that I cannot make the correct packets; I know what they should be, as per http://mc.kev009.com/Protocol. I have to keep it in PHP (sorry) and I don't have the IP/hostname of the connecting client. I have a socket
    
    //Declare Variables
    $host = "localhost";
    $port = 25565;
    $name = "MC-Server";
    $max_slots = 0; //O will render as '???' in MC client
    $used_slots = 0;
    $kick_message = "This server has been shutdown.";
    $number_of_connections = 10;

    //Don't timeout
    set_time_limit(0);

    //Create hex values
    $hex_kick_message = bin2hex($kick_message);
    $hex_name = bin2hex($name);
    $hex_used_slots = bin2hex($used_slots);
    $hex_max_slots = bin2hex($max_slots);

    //Create Socket and Bind
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Failed to create socket. \n");
    $result = socket_bind($socket, $host, $port) or die("Failed to bind socket. " . socket_strerror(socket_last_error()) . "\n");

    //Setup Listener
    $result = socket_listen($socket, 0) or die("Failed to create listener. \n");

    //Needs to recieve more than 1 packet
    $i = 0;
    while (i < $number_of_connections){

      //Create 'Spawn' socket for connections made
      $spawn = socket_accept($socket) or die("Failed to accept incoming connection. \n" );

      //Read and Parse Client input
      $input = socket_read($spawn, 1024) or die("Failed to read client input. \n");

      //Deal with requests
      if ($input = '\xFE'){ //Server Ping
  	  socket_write($spawn, '\xFE\xA7\x' . $hex_name . '\xA7' . $hex_used_slots . '\xA7' . $hex_max_slots);
      } elseif ($input = '\x02'){ //Handshake
	  socket_write($spawn, '\x02\xA7\x2D');
      } elseif ($input = '\x01'){ //Login Request
  	  socket_write($spawn, '\xFF\xA7\x' . $hex_kick_message);
      }
      socket_close($spawn);
      $i = $i++;
    }
    //Close the spawn socket
    socket_close($socket);