private static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;
            EndPointListener     epl  = (EndPointListener)args.UserToken;

#if SSHARP
            CrestronServerSocket accepted = null;
#else
            Socket accepted = null;
#endif
            if (args.SocketError == SocketError.Success)
            {
                accepted          = args.AcceptSocket;
                args.AcceptSocket = null;
            }

            try
            {
                if (epl.sock != null)
#if SSHARP
                { epl.sock.BeginAccept(acceptCallback, Tuple.Create(epl.sock, args)); }
#else
                { epl.sock.AcceptAsync(args); }
#endif
            }
            catch
            {
                if (accepted != null)
                {
                    try
                    {
                        accepted.Close();
                    }
                    catch
                    {
                    }
                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);
            lock (epl.unregistered)
            {
                epl.unregistered[conn] = conn;
            }
            conn.BeginReadRequest();
        }
        public void Close()
        {
            sock.Close();
            lock (unregistered)
            {
                //
                // Clone the list because RemoveConnection can be called from Close
                //
                var connections = new List <HttpConnection> (unregistered.Keys);

                foreach (HttpConnection c in connections)
                {
                    c.Close(true);
                }
                unregistered.Clear();
            }
        }