protected virtual void OnSocketAccepted(SocketAcceptedEventArgs e)
 {
     if (SocketAccepted != null)
     {
         SocketAccepted(this, e);
     }
 }
        void acceptSocket(IAsyncResult result)
        {
            Socket socket = null;

            try
            {
                //New connection
                lock (syncRoot)
                {
                    if (listener == null)
                    {
                        //Server stopped
                        Logger.Debug("TCP Listener: Stopped");
                        return;
                    }

                    socket = listener.EndAcceptSocket(result);
                }

                if (socket != null)
                {
                    //Logger.Debug("TCP Listener: New connection");
                    SocketAcceptedEventArgs e = new SocketAcceptedEventArgs(socket);
                    OnSocketAccepted(e);
                    if (!e.Handled)
                    {
                        socket.Close();
                    }
                }

                lock (syncRoot)
                {
                    if (listener != null)
                    {
                        listener.BeginAcceptSocket(acceptSocket, null);
                    }
                    else
                    {
                        Logger.Debug("TCP Listener: Stopped");
                    }
                }
            }
            catch (SocketException)
            {
                Logger.Debug("TCP Listener: SocketException, stream probably closed");
                if (socket != null)
                {
                    socket.Close();
                }
            }
            catch (Exception ex)
            {
                Logger.Error("TCP Listener: Error accepting new connection -", ex);
                if (socket != null)
                {
                    socket.Close();
                }
            }
        }