internal void Complete(HttpListenerContext context, bool synch)
        {
            if (_forward != null)
            {
                _forward.Complete(context, synch);
                return;
            }
            _synch   = synch;
            _context = context;
            lock (_locker)
            {
                bool authFailure = false;
                try
                {
                    context.AuthenticationSchemes = context._listener.SelectAuthenticationScheme(context);
                }
                catch (OutOfMemoryException oom)
                {
                    context.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
                    _exception = oom;
                }
                catch
                {
                    authFailure = true;
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }

                if (context.AuthenticationSchemes != AuthenticationSchemes.None &&
                    (context.AuthenticationSchemes & AuthenticationSchemes.Anonymous) != AuthenticationSchemes.Anonymous &&
                    (context.AuthenticationSchemes & AuthenticationSchemes.Basic) != AuthenticationSchemes.Basic)
                {
                    authFailure = true;
                    context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                }
                else if (context.AuthenticationSchemes == AuthenticationSchemes.Basic)
                {
                    HttpStatusCode errorCode  = HttpStatusCode.Unauthorized;
                    string         authHeader = context.Request.Headers["Authorization"];
                    if (authHeader == null ||
                        !HttpListenerContext.IsBasicHeader(authHeader) ||
                        authHeader.Length < AuthenticationTypes.Basic.Length + 2 ||
                        !HttpListenerContext.TryParseBasicAuth(authHeader.Substring(AuthenticationTypes.Basic.Length + 1), out errorCode, out string _, out string __))
                    {
                        authFailure = true;
                        context.Response.StatusCode = (int)errorCode;
                        if (errorCode == HttpStatusCode.Unauthorized)
                        {
                            context.Response.Headers["WWW-Authenticate"] = context.AuthenticationSchemes + " realm=\"" + context._listener.Realm + "\"";
                        }
                    }
                }

                if (authFailure)
                {
                    context.Response.OutputStream.Close();
                    IAsyncResult ares = context._listener.BeginGetContext(_cb, _state);
                    _forward = (ListenerAsyncResult)ares;
                    lock (_forward._locker)
                    {
                        if (_handle != null)
                        {
                            _forward._handle = _handle;
                        }
                    }
                    ListenerAsyncResult next = _forward;
                    for (int i = 0; next._forward != null; i++)
                    {
                        if (i > 20)
                        {
                            Complete(new HttpListenerException((int)HttpStatusCode.Unauthorized, SR.net_listener_auth_errors));
                        }
                        next = next._forward;
                    }
                }
                else
                {
                    _completed = true;
                    _synch     = false;

                    if (_handle != null)
                    {
                        _handle.Set();
                    }

                    if (_cb != null)
                    {
                        ThreadPool.UnsafeQueueUserWorkItem(s_invokeCB, this);
                    }
                }
            }
        }