public EndPointListener(IPAddress addr, int port, bool secure)
        {
            if (secure)
            {
                this.secure = secure;
                LoadCertificateAndKey(addr, port);
            }

            endpoint = new IPEndPoint(addr, port);
#if SSHARP
            sock = !addr.Equals(IPAddress.Any) ? new Socket(IPAddress.Any, port, 64, addr.GetAdapterTypeForAddress()) : new Socket(IPAddress.Any, port, 64);
            sock.Listen(64);
#else
            sock = new Socket(addr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            sock.Bind(endpoint);
            sock.Listen(500);
#endif
            SocketAsyncEventArgs args = new SocketAsyncEventArgs();
            args.UserToken  = this;
            args.Completed += OnAccept;
#if SSHARP
            sock.BeginAccept(acceptCallback, Tuple.Create(sock, args));
#else
            sock.AcceptAsync(args);
#endif
            prefixes     = new Hashtable();
            unregistered = new Dictionary <HttpConnection, HttpConnection> ();
        }
        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();
        }