public T Send(string ip, int port, int timeout) { var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var sc = new SocketConnection(client); try { client.Connect(new IPEndPoint(IPAddress.Parse(ip), port)); if (sc.Socket.Connected) { // Send request to AppServer var data = PrepareRequestBuffer(); sc.Socket.Send(data, 0, data.Length, SocketFlags.None); int bytesReceived = sc.Socket.Receive(sc.SocketBuffer, 0, sc.BufferSize, SocketFlags.None); while (bytesReceived > 0) { // There might be more data, so store the data received so far and check for eof if (sc.CopyBuffer(bytesReceived)) { // All the data has arrived. break; } else { bytesReceived = sc.Socket.Receive(sc.SocketBuffer, 0, sc.BufferSize, SocketFlags.None); } } return(BuildResponse(sc)); } else { throw new ApplicationException("Socket was not connected"); } } catch (Exception ex) { Logger.Error("An error has occured while trying to connect to the AppServer. Exception = {0}", LogSource.AppServer, ex); throw new ClientException("An error has occured", ex); } finally { try { sc.Close(); } catch { } try { if (sc.Socket.Connected) { sc.Socket.Shutdown(SocketShutdown.Both); } } catch { } } }