private static ClientCredentials RegisterDevice(Guid applicationId, Uri issuerUri, DeviceUserName userName)
        {
            bool doContinue = true;
            int  attempt    = 1;

            while (doContinue)
            {
                string environment = DiscoverEnvironment(issuerUri);

                LiveDevice device = new LiveDevice()
                {
                    User = userName, Version = 1
                };

                DeviceRegistrationRequest request = new DeviceRegistrationRequest(applicationId, device);

                string url = string.Format(CultureInfo.InvariantCulture, LiveIdConstants.RegistrationEndpointUriFormat,
                                           string.IsNullOrWhiteSpace(environment) ? null : "-" + environment);


                try
                {
                    DeviceRegistrationResponse response = ExecuteRegistrationRequest(url, request);
                    if (!response.IsSuccess)
                    {
                        throw new DeviceRegistrationFailedException(response.RegistrationErrorCode.GetValueOrDefault(), response.ErrorSubCode);
                    }

                    WriteDevice(environment, device);
                }
                catch (Exception error)
                {
                    if (error.Message.ToLower().Contains("unknown"))
                    {
                        if (attempt > 3)
                        {
                            if (MessageBox.Show("Failed to connect 3 times.\r\n\r\nDo you want to retry?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                            {
                                doContinue = false;
                            }
                        }

                        attempt++;
                    }
                    else
                    {
                        throw error;
                    }
                }

                return(device.User.ToClientCredentials());
            }

            return(null);
        }
        private static DeviceRegistrationResponse ExecuteRegistrationRequest(string url, DeviceRegistrationRequest registrationRequest)
        {
            //Create the request that will submit the request to the server
            WebRequest request = WebRequest.Create(url);

            request.ContentType       = "application/soap+xml; charset=UTF-8";
            request.Method            = "POST";
            request.Timeout           = 180000;
            request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

            //Write the envelope to the RequestStream
            using (Stream stream = request.GetRequestStream())
            {
                Serialize(stream, registrationRequest);
            }

            // Read the response into an XmlDocument and return that doc
            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>(stream));
                    }
                }
            }
            catch (WebException ex)
            {
                if (null != ex.Response)
                {
                    using (Stream stream = ex.Response.GetResponseStream())
                    {
                        return(Deserialize <DeviceRegistrationResponse>(stream));
                    }
                }

                throw;
            }
        }