void ReceiveData(IAsyncResult iar)
 {
     try
     {
         ConnectionStateUSB remote = (ConnectionStateUSB)iar.AsyncState;
         remote._conn.EndReceive(iar);
         try
         {
             this.OnReceiveData(remote);
             IsConnected = true;
         }
         catch (Exception ex)
         {
             IsConnected = false;
         }
         //int recv = remote.EndReceive(iar);
         //string stringData = Encoding.ASCII.GetString(data, 0, recv);
         //results.Items.Add(stringData);
         if (remote.Connected)
         {
             remote._conn.BeginReceive(remote._buffer, 0, 0, SocketFlags.None,
                                       new AsyncCallback(ReceiveData), remote);
         }
     }
     catch (Exception ex)
     {
         this.Stop();
     }
 }
    void Connected(IAsyncResult iar)
    {
        try
        {
            client = (Socket)iar.AsyncState;
            client.EndConnect(iar);

            ConnectionStateUSB st = new ConnectionStateUSB();
            st._conn   = client;
            st._buffer = new byte[4];
            //Queue the rest of the job to be executed latter
            //ThreadPool.QueueUserWorkItem(AcceptConnection, st);
            currentST = st;
            //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
            if (client.Connected)
            {
                client.BeginReceive(st._buffer, 0, 0, SocketFlags.None,
                                    new AsyncCallback(ReceiveData), st);
            }
        }
        catch (SocketException e)
        {
            IsConnected = false;
            //conStatus.Text = "Error connecting";
        }
        catch (Exception ex)
        {
            IsConnected = false;
        }
    }
 /// <SUMMARY>
 /// Shutsdown the server
 /// </SUMMARY>
 public void Stop()
 {
     //lock (this)
     //{
     if (newsock != null)
     {
         newsock.Close();
     }
     if (client != null)
     {
         client.Close();
     }
     if (currentST != null)
     {
         currentST._conn.Close();
         currentST = null;
     }
     IsConnected = false;
     //}
 }