/// <summary>
        /// Processes the web exception.
        /// </summary>
        /// <param name="webException">The web exception.</param>
        private void ProcessWebException(WebException webException)
        {
            if (webException.Response != null)
            {
                IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(webException);
                SoapFaultDetails    soapFaultDetails;

                if (httpWebResponse.StatusCode == HttpStatusCode.InternalServerError)
                {
                    // If tracing is enabled, we read the entire response into a MemoryStream so that we
                    // can pass it along to the ITraceListener. Then we parse the response from the
                    // MemoryStream.
                    if (this.Service.IsTraceEnabledFor(TraceFlags.AutodiscoverRequest))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            using (Stream serviceResponseStream = AutodiscoverRequest.GetResponseStream(httpWebResponse))
                            {
                                // Copy response to in-memory stream and reset position to start.
                                EwsUtilities.CopyStream(serviceResponseStream, memoryStream);
                                memoryStream.Position = 0;
                            }

                            this.Service.TraceResponse(httpWebResponse, memoryStream);

                            EwsXmlReader reader = new EwsXmlReader(memoryStream);
                            soapFaultDetails = this.ReadSoapFault(reader);
                        }
                    }
                    else
                    {
                        using (Stream stream = AutodiscoverRequest.GetResponseStream(httpWebResponse))
                        {
                            EwsXmlReader reader = new EwsXmlReader(stream);
                            soapFaultDetails = this.ReadSoapFault(reader);
                        }
                    }

                    if (soapFaultDetails != null)
                    {
                        throw new ServiceResponseException(new ServiceResponse(soapFaultDetails));
                    }
                }
                else
                {
                    this.Service.ProcessHttpErrorResponse(httpWebResponse, webException);
                }
            }
        }
        /// <summary>
        /// Executes this instance.
        /// </summary>
        /// <returns></returns>
        internal AutodiscoverResponse InternalExecute()
        {
            this.Validate();

            try
            {
                IEwsHttpWebRequest request = this.Service.PrepareHttpWebRequestForUrl(this.Url);

                this.Service.TraceHttpRequestHeaders(TraceFlags.AutodiscoverRequestHttpHeaders, request);

                bool needSignature = this.Service.Credentials != null && this.Service.Credentials.NeedSignature;
                bool needTrace     = this.Service.IsTraceEnabledFor(TraceFlags.AutodiscoverRequest);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (EwsServiceXmlWriter writer = new EwsServiceXmlWriter(this.Service, memoryStream))
                    {
                        writer.RequireWSSecurityUtilityNamespace = needSignature;
                        this.WriteSoapRequest(
                            this.Url,
                            writer);
                    }

                    if (needSignature)
                    {
                        this.service.Credentials.Sign(memoryStream);
                    }

                    if (needTrace)
                    {
                        memoryStream.Position = 0;
                        this.Service.TraceXml(TraceFlags.AutodiscoverRequest, memoryStream);
                    }

                    request.SetRequestStream(memoryStream);
                }


                using (IEwsHttpWebResponse webResponse = request.GetResponse())
                {
                    if (AutodiscoverRequest.IsRedirectionResponse(webResponse))
                    {
                        AutodiscoverResponse response = this.CreateRedirectionResponse(webResponse);
                        if (response != null)
                        {
                            return(response);
                        }
                        else
                        {
                            throw new ServiceRemoteException(Strings.InvalidRedirectionResponseReturned);
                        }
                    }

                    using (Stream responseStream = AutodiscoverRequest.GetResponseStream(webResponse))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            // Copy response stream to in-memory stream and reset to start
                            EwsUtilities.CopyStream(responseStream, memoryStream);
                            memoryStream.Position = 0;

                            this.Service.TraceResponse(webResponse, memoryStream);

                            EwsXmlReader ewsXmlReader = new EwsXmlReader(memoryStream);

                            // WCF may not generate an XML declaration.
                            ewsXmlReader.Read();
                            if (ewsXmlReader.NodeType == XmlNodeType.XmlDeclaration)
                            {
                                ewsXmlReader.ReadStartElement(XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName);
                            }
                            else if ((ewsXmlReader.NodeType != XmlNodeType.Element) || (ewsXmlReader.LocalName != XmlElementNames.SOAPEnvelopeElementName) || (ewsXmlReader.NamespaceUri != EwsUtilities.GetNamespaceUri(XmlNamespace.Soap)))
                            {
                                throw new ServiceXmlDeserializationException(Strings.InvalidAutodiscoverServiceResponse);
                            }

                            this.ReadSoapHeaders(ewsXmlReader);

                            AutodiscoverResponse response = this.ReadSoapBody(ewsXmlReader);

                            ewsXmlReader.ReadEndElement(XmlNamespace.Soap, XmlElementNames.SOAPEnvelopeElementName);

                            if (response.ErrorCode == AutodiscoverErrorCode.NoError)
                            {
                                return(response);
                            }
                            else
                            {
                                throw new AutodiscoverResponseException(response.ErrorCode, response.ErrorMessage);
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    IEwsHttpWebResponse httpWebResponse = this.Service.HttpWebRequestFactory.CreateExceptionResponse(ex);

                    if (AutodiscoverRequest.IsRedirectionResponse(httpWebResponse))
                    {
                        this.Service.ProcessHttpResponseHeaders(
                            TraceFlags.AutodiscoverResponseHttpHeaders,
                            httpWebResponse);

                        AutodiscoverResponse response = this.CreateRedirectionResponse(httpWebResponse);
                        if (response != null)
                        {
                            return(response);
                        }
                    }
                    else
                    {
                        this.ProcessWebException(ex);
                    }
                }

                // Wrap exception if the above code block didn't throw
                throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex);
            }
            catch (XmlException ex)
            {
                this.Service.TraceMessage(
                    TraceFlags.AutodiscoverConfiguration,
                    string.Format("XML parsing error: {0}", ex.Message));

                // Wrap exception
                throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex);
            }
            catch (IOException ex)
            {
                this.Service.TraceMessage(
                    TraceFlags.AutodiscoverConfiguration,
                    string.Format("I/O error: {0}", ex.Message));

                // Wrap exception
                throw new ServiceRequestException(string.Format(Strings.ServiceRequestFailed, ex.Message), ex);
            }
        }