private CrmServiceClient ConnectOnline(bool isOffice365, ConnectionSettings settings)
        {
            var securePassword = new SecureString();
            foreach (char c in settings.Password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();

            return new CrmServiceClient(settings.Username, securePassword, GetOnlineRegion(hostName), orga, true, useSsl, isOffice365: isOffice365);
        }
        private void Connect()
        {
            visitedPath.Add(pnlWaiting.Name);

            var settings = new ConnectionSettings
            {
                Domain = txtDomain.Text,
                Username = txtUsername.Text,
                Password = txtPassword.Text,
            };

            if (settings.Password == "@@PASSWORD@@")
            {
            }

            var bw = new BackgroundWorker();
            bw.DoWork += (bwSender, evt) =>
            {
                var bwSettings = (ConnectionSettings)evt.Argument;
                CrmServiceClient crmSvc;

                if (isOnline)
                {
                    Task<CrmServiceClient>[] taskArray =
                {
                    Task<CrmServiceClient>.Factory.StartNew(() => ConnectOnline(true, bwSettings)),
                    Task<CrmServiceClient>.Factory.StartNew(() => ConnectOnline(false, bwSettings)),
                };

                    taskArray[0].Wait();
                    taskArray[1].Wait();

                    var goodResult = taskArray.FirstOrDefault(t => t.Result.IsReady);
                    if (goodResult != null)
                    {
                        crmSvc = goodResult.Result;

                        isOffice365 = taskArray[0].Result.IsReady;
                    }
                    else
                    {
                        crmSvc = taskArray.First().Result;
                    }
                }
                else if (isIfd)
                {
                    crmSvc = new CrmServiceClient(
                       new NetworkCredential(bwSettings.Username, bwSettings.Password, bwSettings.Domain), AuthenticationType.IFD, hostName, hostPort,
                       orga, true, useSsl);
                }
                else
                {
                    if (orga == null)
                    {
                        MessageBox.Show(this, Resources.ConnectionWizard_UnableToDetermineOrganizationFromUrl, Resources.ConnectionWizard_WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        return;
                    }

                    NetworkCredential credential;
                    if (chkUseIntegratedAuthentication.Checked)
                    {
                        credential = CredentialCache.DefaultNetworkCredentials;
                    }
                    else
                    {
                        credential = new NetworkCredential(bwSettings.Username, bwSettings.Password, bwSettings.Domain);
                    }
                    crmSvc = new CrmServiceClient(credential, AuthenticationType.AD, hostName, hostPort, orga, true, useSsl);
                }

                evt.Result = crmSvc;
            };
            bw.RunWorkerCompleted += (bwSender, evt) =>
            {
                if (evt.Error != null)
                {
                    lblDescription.Text = Resources.ConnectionWizard_ErrorHeaderDescription;

                    lblError.Text = evt.Error.Message;
                    DisplayPanel(pnlError);

                    return;
                }

                CrmServiceClient crmSvc = (CrmServiceClient)evt.Result;

                if (!string.IsNullOrEmpty(crmSvc.LastCrmError))
                {
                    lblDescription.Text = Resources.ConnectionWizard_ErrorHeaderDescription;

                    lblError.Text = crmSvc.LastCrmError;
                    DisplayPanel(pnlError);

                    return;
                }

                lblDescription.Text = Resources.ConnectionWizard_SuccessHeaderDescription;

                DisplayPanel(pnlConnected);
                txtConnectionName.Focus();

                serviceClient = crmSvc;
            };
            bw.RunWorkerAsync(settings);
        }