private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Client = (Socket)ar.AsyncState;

                // Complete the connection.
                Client.EndConnect(ar);

                RamLogger.Log("Socket connected to " + Client.RemoteEndPoint.ToString());
                Console.WriteLine("Socket connected to {0}",
                                  Client.RemoteEndPoint.ToString());

                // Signal that the connection has been made.
                connectDone.Set();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                LastRaisedException = ex;

                connectDone.Set();
            }
        }
        public void Disconnect()
        {
            try
            {
                Client.Shutdown(SocketShutdown.Both);
                Client.Close();

                RamLogger.Log("Disconnected from server");
            }
            catch (Exception ex)
            {
                if (ex is ObjectDisposedException)
                {
                    return;
                }

                throw ex;
            }
        }
        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.
                Client = (Socket)ar.AsyncState;

                // Complete sending the data to the remote device.
                int bytesSent = Client.EndSend(ar);
                RamLogger.Log("Sent " + bytesSent + " bytes to server");
                Console.WriteLine("Sent {0} bytes to server.", bytesSent);

                // Signal that all bytes have been sent.
                sendDone.Set();
            }
            catch (Exception e)
            {
                LastRaisedException = e;

                Disconnect();

                Console.WriteLine(e.ToString());
            }
        }