title: Socket Programming - Sending/Receiving hex and strings
body: <p>I have the following code in C#:</p>
  
  <pre><code>Console.WriteLine("Connecting to server...");
  TcpClient client = new TcpClient("127.0.0.1", 25565);
  client.Client.Send(BitConverter.GetBytes(0x02));
  client.Client.Send(BitConverter.GetBytes(0x0005));
  client.Client.Send(Encoding.UTF8.GetBytes("wedtm"));
  Console.Write("{0:x2}", client.GetStream().ReadByte());
  </code></pre>
  
  <p>For the life of me, I can't figure out how to transpose this to ruby. Any help here?</p>
  
  <p>This is what I have so far, but it's not working as expected:</p>
  
  <pre><code>require 'socket'
  s = TCPSocket.open("127.0.0.1", 25565)
  s.write(0x02)
  s.write(0x0005)
  s.write("wedtm".bytes)
  response = s.recvfrom(2)
  puts "Response Size #{response.size}: #{response.to_s}"
  </code></pre>
  
  <p>The response should be 0x02</p>
  
  <p>EDIT:</p>
  
  <p>I'm assuming I have to use String#unpack on this, however, I can't figure out how to get "wedtm" to output to the appropriate <code>\x000\x000\x000\x000</code> format.</p>