internal void Complete(HttpListenerContext context, bool syncCompleted)
        {
            var listener = context.Listener;
            var scheme   = listener.SelectAuthenticationScheme(context);

            if (scheme == AuthenticationSchemes.None)
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                listener.BeginGetContext(this);

                return;
            }

            var header = context.Request.Headers ["Authorization"];

            if (scheme == AuthenticationSchemes.Basic &&
                (header == null ||
                 !header.StartsWith("basic", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(
                    HttpUtility.CreateBasicAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);

                return;
            }

            if (scheme == AuthenticationSchemes.Digest &&
                (header == null ||
                 !header.StartsWith("digest", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(
                    HttpUtility.CreateDigestAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);

                return;
            }

            _context       = context;
            _syncCompleted = syncCompleted;

            lock (_sync) {
                _completed = true;
                if (_waitHandle != null)
                {
                    _waitHandle.Set();
                }

                if (_callback != null)
                {
                    ThreadPool.UnsafeQueueUserWorkItem(invokeCallback, this);
                }
            }
        }
        internal void Complete(HttpListenerContext context, bool syncCompleted)
        {
            HttpListener          listener = context.Listener;
            AuthenticationSchemes authenticationSchemes = listener.SelectAuthenticationScheme(context);

            if (authenticationSchemes == AuthenticationSchemes.None)
            {
                context.Response.Close(HttpStatusCode.Forbidden);
                listener.BeginGetContext(this);
                return;
            }
            string text = context.Request.Headers["Authorization"];

            if (authenticationSchemes == AuthenticationSchemes.Basic && (text == null || !text.StartsWith("basic", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(HttpUtility.CreateBasicAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);
                return;
            }
            if (authenticationSchemes == AuthenticationSchemes.Digest && (text == null || !text.StartsWith("digest", StringComparison.OrdinalIgnoreCase)))
            {
                context.Response.CloseWithAuthChallenge(HttpUtility.CreateDigestAuthChallenge(listener.Realm));
                listener.BeginGetContext(this);
                return;
            }
            this._context       = context;
            this._syncCompleted = syncCompleted;
            object sync = this._sync;

            lock (sync)
            {
                this._completed = true;
                if (this._waitHandle != null)
                {
                    this._waitHandle.Set();
                }
                if (this._callback != null)
                {
                    ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(ListenerAsyncResult.invokeCallback), this);
                }
            }
        }