private void ReceiveCallback(IAsyncResult ar)
 {
     try {
         // Retrieve the state object and the client socket
         // from the asynchronous state object.
         StateObject state = (StateObject)ar.AsyncState;
         Client = state.workSocket;
         // Read data from the remote device.
         int bytesRead = Client.EndReceive(ar);
         if (bytesRead > 0)
         {
             // There might be more data, so store the data received so far.
             state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
         }
         if (bytesRead < state.BufferSize)
         {
             //  Get the rest of the data.
             Client.BeginReceive(state.buffer, 0, state.BufferSize, 0,
                                 ReceiveCallback, state);
         }
         else
         {
             // All the data has arrived; put it in response.
             if (state.sb.Length >= state.BufferSize)
             {
                 Received?.Invoke(this, state.sb.ToString());
             }
         }
     } catch (Exception e) {
         ErrorAccrued?.Invoke(this, new ErrorEventArgs(e));
     }
 }
 private void ConnectCallback(IAsyncResult ar)
 {
     try {
         // Retrieve the socket from the state object.
         Client = (Socket)ar.AsyncState;
         // Complete the connection.
         Client.EndConnect(ar);
         Connected?.Invoke(this, EventArgs.Empty);
     } catch (Exception e) {
         ErrorAccrued?.Invoke(this, new ErrorEventArgs(e));
     }
 }
        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);
                Sent?.Invoke(this, bytesSent);
            } catch (Exception e) {
                ErrorAccrued?.Invoke(this, new ErrorEventArgs(e));
            }
        }
 public void Connect()
 {
     if (Client.Connected)
     {
         return;
     }
     try
     {
         Client.Connect(Host, Port);
         Connected?.Invoke(this, EventArgs.Empty);
     }
     catch (Exception e)
     {
         ErrorAccrued?.Invoke(this, new ErrorEventArgs(e));
     }
 }
        public void Receive(int bytes)
        {
            try
            {
                // Create the state object.
                StateObject state = new StateObject {
                    workSocket = Client, BufferSize = bytes
                };

                // Begin receiving the data from the remote device.
                Client.BeginReceive(state.buffer, 0,
                                    state.BufferSize,
                                    0, ReceiveCallback, state);
            }
            catch (Exception e)
            {
                ErrorAccrued?.Invoke(this, new ErrorEventArgs(e));
            }
        }