internal static string ExecuteAnonymousLyncAutodiscoverRedirect(string requestUrl, int redirectCount)
        {
            string empty = string.Empty;

            if (redirectCount >= 10)
            {
                return(empty);
            }
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl);

            httpWebRequest.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
            LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();

            lyncAutodiscoverRequestState.Request = httpWebRequest;
            try
            {
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK && LyncAutodiscoverWorker.GetAuthenticatedAutodiscoverEndpointFromHttpWebResponse(httpWebResponse, redirectCount, out empty))
                {
                    return(empty);
                }
            }
            catch (WebException)
            {
            }
            return(empty);
        }
        internal static void ProcessLyncAnonymousAutodiscoverResponse(IAsyncResult asyncResult)
        {
            LyncAutodiscoverRequestState lyncAutodiscoverRequestState = (LyncAutodiscoverRequestState)asyncResult.AsyncState;
            HttpWebRequest request = lyncAutodiscoverRequestState.Request;

            if (request != null)
            {
                try
                {
                    HttpWebResponse httpWebResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
                    string          targetUrl;
                    if (httpWebResponse.StatusCode == HttpStatusCode.OK && LyncAutodiscoverWorker.GetAuthenticatedAutodiscoverEndpointFromHttpWebResponse(httpWebResponse, 0, out targetUrl))
                    {
                        lyncAutodiscoverRequestState.TargetUrl = targetUrl;
                    }
                }
                catch (WebException)
                {
                    lyncAutodiscoverRequestState.TargetUrl = string.Empty;
                }
                LyncAutodiscoverWorker.allDone.Set();
            }
        }
        internal static string ExecuteAnonymousLyncAutodiscoverRequests(bool sendInternal, string domain, string user)
        {
            LyncAutodiscoverWorker.details.AppendLine(string.Format("Executing: ExecuteAnonymousLyncAutodiscoverRequests", new object[0]));
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(LyncAutodiscoverWorker.CertificateValidationCallBack);
            string result = string.Empty;
            string arg    = string.Format("?sipuri={0}", user);

            try
            {
                string text  = sendInternal ? string.Format("http://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscoverinternal", domain, arg) : string.Format("http://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscover", domain, arg);
                string text2 = sendInternal ? string.Format("https://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscoverinternal", domain, arg) : string.Format("https://{0}.{1}/autodiscover/autodiscoverservice.svc/root{2}", "lyncdiscover", domain, arg);
                LyncAutodiscoverWorker.details.AppendLine(string.Format("HttpUrl:{0}", text));
                LyncAutodiscoverWorker.details.AppendLine(string.Format("HttpsUrl:{0}", text2));
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(text);
                httpWebRequest.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState.Request = httpWebRequest;
                HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(text2);
                httpWebRequest2.Accept = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState2 = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState2.Request = httpWebRequest2;
                LyncAutodiscoverWorker.allDone.Reset();
                IAsyncResult asyncResult  = httpWebRequest.BeginGetResponse(new AsyncCallback(LyncAutodiscoverWorker.ProcessLyncAnonymousAutodiscoverResponse), lyncAutodiscoverRequestState);
                IAsyncResult asyncResult2 = httpWebRequest2.BeginGetResponse(new AsyncCallback(LyncAutodiscoverWorker.ProcessLyncAnonymousAutodiscoverResponse), lyncAutodiscoverRequestState2);
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(LyncAutodiscoverWorker.TimeoutCallback), httpWebRequest, 120000, true);
                ThreadPool.RegisterWaitForSingleObject(asyncResult2.AsyncWaitHandle, new WaitOrTimerCallback(LyncAutodiscoverWorker.TimeoutCallback), httpWebRequest2, 120000, true);
                LyncAutodiscoverWorker.allDone.WaitOne();
                if (lyncAutodiscoverRequestState.Response != null)
                {
                    lyncAutodiscoverRequestState.Response.Close();
                }
                if (lyncAutodiscoverRequestState2.Response != null)
                {
                    lyncAutodiscoverRequestState2.Response.Close();
                }
                if (string.IsNullOrEmpty(lyncAutodiscoverRequestState.TargetUrl) && string.IsNullOrEmpty(lyncAutodiscoverRequestState2.TargetUrl))
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Both http and https responses are empty.", new object[0]));
                    return(result);
                }
                if (!string.IsNullOrEmpty(lyncAutodiscoverRequestState.TargetUrl))
                {
                    if (lyncAutodiscoverRequestState.IsRedirect)
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("Redirecting to {0}.", lyncAutodiscoverRequestState.TargetUrl));
                        result = LyncAutodiscoverWorker.ExecuteAnonymousLyncAutodiscoverRedirect(lyncAutodiscoverRequestState.TargetUrl, 0);
                    }
                    else
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("Authenticated Endpoint: {0}.", lyncAutodiscoverRequestState.TargetUrl));
                        result = lyncAutodiscoverRequestState.TargetUrl;
                    }
                }
                else if (lyncAutodiscoverRequestState2.IsRedirect)
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Redirecting to {0}.", lyncAutodiscoverRequestState.TargetUrl));
                    result = LyncAutodiscoverWorker.ExecuteAnonymousLyncAutodiscoverRedirect(lyncAutodiscoverRequestState.TargetUrl, 0);
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Authenticated Endpoint: {0}.", lyncAutodiscoverRequestState.TargetUrl));
                    result = lyncAutodiscoverRequestState2.TargetUrl;
                }
            }
            catch (WebException ex)
            {
                LyncAutodiscoverWorker.details.AppendLine(string.Format("Exception: {0}.", ex.ToString()));
            }
            return(result);
        }
        internal static LyncAutodiscoverResult GetUcwaUrl(string authenticatedEndpointUrl, ICredentials credentials)
        {
            LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrl");
            LyncAutodiscoverResult lyncAutodiscoverResult = new LyncAutodiscoverResult();

            lyncAutodiscoverResult.IsUcwaSupported  = false;
            lyncAutodiscoverResult.UcwaDiscoveryUrl = string.Empty;
            string text = string.Empty;

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(LyncAutodiscoverWorker.CertificateValidationCallBack);
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(authenticatedEndpointUrl);
                httpWebRequest.Accept      = "application/vnd.microsoft.rtc.autodiscover+xml;v=1";
                httpWebRequest.Timeout     = 120000;
                httpWebRequest.Credentials = credentials;
                LyncAutodiscoverRequestState lyncAutodiscoverRequestState = new LyncAutodiscoverRequestState();
                lyncAutodiscoverRequestState.Request = httpWebRequest;
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse.ToString();
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine("Response is NULL");
                }
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    LyncAutodiscoverWorker.details.AppendLine("Calling GetUcwaUrlFromHttpWebResponse to get ucwa URL.");
                    if (LyncAutodiscoverWorker.GetUcwaUrlFromHttpWebResponse(httpWebResponse, credentials, 0, out text))
                    {
                        LyncAutodiscoverWorker.details.AppendLine(string.Format("ucwa URL: {0}", text));
                        lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
                        return(lyncAutodiscoverResult);
                    }
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("unable to find ucwa URL from response.", new object[0]));
                }
                else
                {
                    LyncAutodiscoverWorker.details.AppendLine(string.Format("Http status code: {0}", httpWebResponse.StatusCode));
                }
            }
            catch (WebException ex)
            {
                LyncAutodiscoverWorker.details.AppendLine(string.Format("Exception: {0}", ex.ToString()));
                HttpWebResponse httpWebResponse2 = ex.Response as HttpWebResponse;
                if (httpWebResponse2 != null)
                {
                    lyncAutodiscoverResult.Response = httpWebResponse2.ToString();
                    HttpStatusCode statusCode = httpWebResponse2.StatusCode;
                    if (statusCode == HttpStatusCode.Unauthorized)
                    {
                        text = ((ex.InnerException == null) ? ex.Message : ex.InnerException.Message);
                    }
                }
            }
            lyncAutodiscoverResult.UcwaDiscoveryUrl = text;
            lyncAutodiscoverResult.IsUcwaSupported  = !string.IsNullOrWhiteSpace(text);
            lyncAutodiscoverResult.DiagnosticInfo   = LyncAutodiscoverWorker.details.ToString();
            LyncAutodiscoverWorker.details.Clear();
            return(lyncAutodiscoverResult);
        }