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();
        }
    private static void processAccepted (Socket socket, EndPointListener listener)
    {
      HttpConnection conn = null;
      try {
        conn = new HttpConnection (socket, listener);
        lock (listener._unregisteredSync)
          listener._unregistered[conn] = conn;

        conn.BeginReadRequest ();
      }
      catch {
        if (conn != null) {
          conn.Close (true);
          return;
        }

        socket.Close ();
      }
    }
		static void OnAccept (IAsyncResult ares)
		{
			EndPointListener epl = (EndPointListener) ares.AsyncState;
			Socket accepted = null;
			try {
				accepted = epl.sock.EndAccept (ares);
			} catch {
				// Anything to do here?
			} finally {
				try {
					epl.sock.BeginAccept (OnAccept, epl);
				} catch {
					if (accepted != null) {
						try {
							accepted.Close ();
						} catch {}
						accepted = null;
					}
				} 
			}

			if (accepted == null)
				return;

			if (epl.secure && (epl.key == null)) {
				accepted.Close ();
				return;
			}
			HttpConnection conn = new HttpConnection (accepted, epl);
			conn.BeginReadRequest ();
		}
		static void OnAccept (object sender, EventArgs e)
		{
			SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
			EndPointListener epl = (EndPointListener) args.UserToken;
			Socket accepted = null;
			if (args.SocketError == SocketError.Success) {
				accepted = args.AcceptSocket;
				args.AcceptSocket = null;
			}

			try {
				if (epl.sock != null)
					epl.sock.AcceptAsync (args);
			} 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 ();
		}
    private static void onAccept (object sender, EventArgs e)
    {
      var args = (SocketAsyncEventArgs) e;
      var listener = (EndPointListener) args.UserToken;
      Socket accepted = null;
      if (args.SocketError == SocketError.Success) {
        accepted = args.AcceptSocket;
        args.AcceptSocket = null;
      }

      try {
        listener._socket.AcceptAsync (args);
      }
      catch {
        if (accepted != null)
          accepted.Close ();

        return;
      }

      if (accepted == null)
        return;

      HttpConnection conn = null;
      try {
        conn = new HttpConnection (accepted, listener);
        lock (((ICollection) listener._unregistered).SyncRoot)
          listener._unregistered [conn] = conn;

        conn.BeginReadRequest ();
      }
      catch {
        if (conn != null) {
          conn.Close (true);
          return;
        }

        accepted.Close ();
      }
    }
        private void ProcessAccept(Socket accepted)
        {
            try
            {
                var listener = this;

                if (listener.secure && listener.cert == null)
                {
                    accepted.Close();
                    return;
                }

                var connectionId = Guid.NewGuid().ToString("N");

                HttpConnection conn = new HttpConnection(_logger, accepted, listener, listener.secure, listener.cert, connectionId);
                //_logger.Debug("Adding unregistered connection to {0}. Id: {1}", accepted.RemoteEndPoint, connectionId);
                lock (listener.unregistered)
                {
                    listener.unregistered[conn] = conn;
                }
                conn.BeginReadRequest();
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error in ProcessAccept", ex);
            }
        }
        private static void ProcessAccept(SocketAsyncEventArgs args)
        {
            Socket accepted = null;
            if (args.SocketError == SocketError.Success)
                accepted = args.AcceptSocket;

            var epl = (EndPointListener) args.UserToken;

            Accept(epl._sock, args, ref accepted);
            if (accepted == null)
                return;

            if (epl._secure && epl._cert == null)
            {
                accepted.Dispose();
                return;
            }
            var conn = new HttpConnection(accepted, epl, epl._secure, epl._cert);
            lock (epl._unregistered)
            {
                epl._unregistered[conn] = conn;
            }
            conn.BeginReadRequest();
        }