private bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
            {
                return(true);
            }

            string certificateIssuer = certificate.Issuer;

            lock (_syncObj)
            {
                if (trustedCertificateIssuersContainer.TrustedCertificateIssuers != null && trustedCertificateIssuersContainer.TrustedCertificateIssuers.Contains(certificateIssuer))
                {
                    return(true);
                }

                var    request = sender as HttpWebRequest;
                string server  = request == null ? null : " \"" + request.Address.Host + "\"";
                string msg     = string.Format("{0} cannot verify the identity of the server{1}, due to a certificate problem. the server could be trying to trick you. Would you like to continue to the server?"
                                               + "\r\n\r\nCertificate: {2}"
                                               + "\r\n\rSSL error: {3}", ProgramInfo.Name, server, certificate, sslPolicyErrors);

                var showCertificateButton = new Button()
                {
                    Text  = "Show certificate",
                    Width = 100
                };
                showCertificateButton.Click += (s, e) =>
                {
                    var x509Certificate2 = new X509Certificate2(certificate);
                    X509Certificate2UI.DisplayCertificate(x509Certificate2);
                };

                string btnContinueAnyWay = "Continue Anyway";
                string btnAddToExclusionInThisSession = "Add to exclusion in this session";
                string btnCancel     = "Cancel";
                var    pressedButton = AdvancedMessageBox.Show(msg, "Invalid certificate", showCertificateButton, MessageBoxIcon.Warning, btnContinueAnyWay, btnAddToExclusionInThisSession, btnCancel);

                if (pressedButton == btnContinueAnyWay)
                {
                    return(true);
                }
                else if (pressedButton == btnAddToExclusionInThisSession)
                {
                    var trustedCertificateIssuers = trustedCertificateIssuersContainer.TrustedCertificateIssuers;
                    if (trustedCertificateIssuers == null)
                    {
                        trustedCertificateIssuers = new HashSet <string>();
                    }
                    trustedCertificateIssuers.Add(certificateIssuer);
                    trustedCertificateIssuersContainer.TrustedCertificateIssuers = trustedCertificateIssuers;
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Performs the build operation.
        /// </summary>
        /// <param name="isLocalDeployment">if set to <c>true</c> [is local deployment].</param>
        /// <param name="currentVersionForlocalDeploy">The current version forlocal deploy.</param>
        /// <returns>
        /// true if build is successful
        /// </returns>
        private bool PerformBuildOperation(bool isLocalDeployment, string currentVersionForlocalDeploy = "")
        {
            bool buildSuccessful = true;
            int stepNumber = 10;

            if (isLocalDeployment)
            {
                if (string.IsNullOrEmpty(currentVersionForlocalDeploy))
                {
                    throw new ArgumentNullException("Unable to get next successful version to deploy.");
                }

                try
                {
                    this.AssignCloudArgsNoBuild(currentVersionForlocalDeploy);
                }
                catch (IOException ex)
                {
                    ex.ShowUIException();
                    return false;
                }

                return true;
            }

            // Initialize
            this.backgroundWorker.ReportProgress(++stepNumber, "Initializing build parameters....");
            using (BuildTasks tasks = new BuildTasks(this.buildArgs))
            {
                try
                {
                    this.backgroundWorker.ReportProgress(++stepNumber, "Creating temporary TFS workspace....");
                    tasks.CreateDedicatedTfsWorkSpace();

                    this.backgroundWorker.ReportProgress(++stepNumber, "Downloading code for TFS label....");
                    tasks.GetTfsLabelCodeToLocal(txtTFSLabelName.Text);

                    this.backgroundWorker.ReportProgress(++stepNumber, "Compiling and building solution.....");
                    if (tasks.ExecMsBuild())
                    {
                        using (AdvancedMessageBox adv = new AdvancedMessageBox("Build suceeded but has warnings. Do you want to continue?", ReportStatus.Information, true))
                        {
                            this.Invoke((MethodInvoker)delegate
                            {
                                if (DialogResult.Cancel == adv.ShowDialog(this))
                                {
                                    this.AssignCloudArgsAfterBuild(tasks.MSbuildLogPath, tasks.TFLogPath, tasks.AzurePackagesPath);
                                    throw new OperationCanceledException("Operation cancelled.");
                                }
                            });
                        }
                    }

                    this.AssignCloudArgsAfterBuild(tasks.MSbuildLogPath, tasks.TFLogPath, tasks.AzurePackagesPath);
                    this.ShowRequiredDialogs(tasks.AzurePackagesPath);
                }
                catch (FormatException formatexception)
                {
                    buildSuccessful = false;
                    this.Invoke((MethodInvoker)(() => formatexception.ShowUIException()));
                    this.backgroundWorker.ReportProgress(stepNumber, ReportStatus.Fail);
                }
                catch (OperationCanceledException exception)
                {
                    buildSuccessful = false;
                    this.Invoke((MethodInvoker)(() => exception.ShowUIException()));
                    this.backgroundWorker.ReportProgress(stepNumber, ReportStatus.Fail);
                }
                catch (Exception exception)
                {
                    buildSuccessful = false;
                    this.Invoke((MethodInvoker)(() => exception.ShowGenericException("Error in building application.")));
                    this.backgroundWorker.ReportProgress(stepNumber, ReportStatus.Fail);
                }
                finally
                {
                    // in case there is an exception and we are came here directly then check for log paths to enable.
                    if (this.msbuildLogPath == null)
                    {
                        this.msbuildLogPath = tasks.MSbuildLogPath;
                    }

                    if (this.teamfoundationExeLogPath == null)
                    {
                        this.teamfoundationExeLogPath = tasks.TFLogPath;
                    }

                    this.backgroundWorker.ReportProgress(++stepNumber, "Deleting temporary TFS workspace....");
                    tasks.DeleteDedicatedTfsWorkSpace();
                }
            }

            // enable ths user to see log paths
            this.Invoke((MethodInvoker)delegate
            {
                this.btnViewBuildLog.Enabled = File.Exists(this.msbuildLogPath);
                this.btnViewTFLog.Enabled = File.Exists(this.teamfoundationExeLogPath);
            });

            return buildSuccessful;
        }
        /// <summary>
        /// Handles the Selecting event of the tabControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.TabControlCancelEventArgs"/> instance containing the event data.</param>
        private void TabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            using(AdvancedMessageBox advbox = new AdvancedMessageBox("Are you sure you want to cancel current operation?", ReportStatus.Information, true))
            {
                if (DialogResult.OK == advbox.ShowDialog())
                {
                    this.ResetTreeViewNodes(this.tvCheckList.Nodes["10"]);
                    this.ResetTreeViewNodes(this.tvCheckList.Nodes["20"]);
                    this.progressBar.Value = this.progressBar.Minimum;
                    this.btnViewBuildLog.Enabled = this.btnViewDeployLog.Enabled = this.btnViewTFLog.Enabled = false;
                    this.toolStripStatusLabel.Text = string.Empty;
                    this.rollbackVersionToLog = string.Empty;
                    this.pkgNameForLocalDeployment = string.Empty;
                    this.buildArgs = null;

                    switch (e.TabPageIndex)
                    {
                        case 0:
                            this.txtConfigPath.Text = this.txtPackagePath.Text = string.Empty;
                            break;
                        case 1:
                            this.cbxServerName.SelectedIndex = -1;
                            txtTFSLabelName.Text = txtTFSDefaultCollection.Text = txtSolutionName.Text = string.Empty;
                            btnLoadSolutions.Enabled = false;
                            this.btnLoadTFSlabel.Enabled = false;
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            this.BringToFront();
        }