// Called when a SendAsync operation completes
        private void ProcessSend(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                //Read data sent from the server
                Socket sock = e.UserToken as Socket;

                sock.ReceiveAsync(e);
            }
            else
            {
                ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                args.response = e.SocketError.ToString();
                args.isError = true;
                OnResponseReceived(args);
            }
        }
        // Called when a ConnectAsync operation completes
        private void ProcessConnect(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Successfully connected to the server
                // Send data to the server
                byte[] buffer = Encoding.UTF8.GetBytes(dataIn + "<EOF>");
                e.SetBuffer(buffer, 0, buffer.Length);
                Socket sock = e.UserToken as Socket;
                sock.SendAsync(e);
            }
            else
            {
                ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                args.response = e.SocketError.ToString();
                args.isError = true;
                OnResponseReceived(args);

            }
        }
        // Called when a ReceiveAsync operation completes
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                // Received data from server
                string dataFromServer = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred);

                Socket sock = e.UserToken as Socket;
                sock.Shutdown(SocketShutdown.Send);
                sock.Close();

                // Respond to the client in the UI thread to tell him that data was received
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    ResponseReceivedEventArgs args = new ResponseReceivedEventArgs();
                    args.response = dataFromServer;
                    OnResponseReceived(args);
                });

            }
            else
            {
                throw new SocketException((int)e.SocketError);
            }
        }
 // Invoke the ResponseReceived event
 protected void OnResponseReceived(ResponseReceivedEventArgs e)
 {
     if (ResponseReceived != null)
         ResponseReceived(this, e);
 }