public void ServerRequest(ClientCallback callback, int mtype, string req) { IPAddress ad = Dns.GetHostAddresses("dankdankdank.ddns.net")[0]; MessageConstants.InitValues(); TcpClient client = new TcpClient(); IPEndPoint serverEndPoint = new IPEndPoint(ad, 42666); client.Connect(serverEndPoint); NetworkStream clientStream = client.GetStream(); ASCIIEncoding encoder = new ASCIIEncoding(); byte[] buffer = encoder.GetBytes(MessageConstants.MessageTypes[mtype] + req + MessageConstants.completeToken); clientStream.Write(buffer, 0, buffer.Length); clientStream.Flush(); TcpClient tcpClient = (TcpClient)client; clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; string reply = ""; bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); //keep reading until the message is done; while (bytesRead != 0) { //message has successfully been received encoder = new ASCIIEncoding(); reply += encoder.GetString(message, 0, bytesRead); if (reply.Substring(reply.Length - MessageConstants.completeToken.Length, MessageConstants.completeToken.Length) == MessageConstants.completeToken) { break; } if (bytesRead < 4096) { // break; } bytesRead = clientStream.Read(message, 0, 4096); } } catch { //a socket error has occured return; } if (bytesRead == 0) { //the client has disconnected from the server return; } List <List <string> > transmission = Decodetransmission(reply); if (transmission[0][0] == MessageConstants.MessageTypes[mtype]) { callback(transmission); } }