/// <summary>
        /// Creates a new NetworkSettings objects from the currently used settings.
        /// </summary>
        /// <returns></returns>
        private NetworkSettings CurrentSettings()
        {
            NetworkSettings netSettings = new NetworkSettings(this.wiretapServer.Host, this.wiretapServer.Port.ToString(),
                                                              WebRequest.DefaultWebProxy != this.IEProxy, "", "");

            if (netSettings.useCustomProxy)
            {
                netSettings.proxyHost = ((WebProxy)WebRequest.DefaultWebProxy).Address.Host;
                netSettings.proxyPort = ((WebProxy)WebRequest.DefaultWebProxy).Address.Port.ToString();
            }

            return(netSettings);
        }
        /// <summary>
        /// Uses the supplied network settings to perform a HEAD request on the wiretap server
        /// home page. Doesn't affect the currently applied network settings.
        /// </summary>
        /// <param name="settings">The NetworkSettings to test.</param>
        /// <returns>The error message if any setting or network error occurs, or String.Empty on success.</returns>
        public string TestConnection(NetworkSettings settings)
        {
            proxyCredDialog.AlwaysDisplay = true;
            if (!proxyCredDialog.SaveChecked)
            {
                proxyCredDialog.Password = "";
            }


            // store prev settings so we can revert after test

            NetworkSettings prevNetSettings = CurrentSettings();


            try
            {
                // apply new settings

                LoadSettings(settings);

                if (WebRequest.DefaultWebProxy != null)
                {
                    WebRequest.DefaultWebProxy.Credentials = null; // force prompt
                }
                // do test

                PerformWebRequest(WebRequest_TestConnection, null);
            }
            catch (Exception ex)
            {
                LoadSettings(prevNetSettings); // revert

                if (ex is WebException && ((WebException)ex).Status == WebExceptionStatus.ProtocolError)
                {
                    return(String.Format("HTTP {0}, {1}",
                                         (int)((HttpWebResponse)((WebException)ex).Response).StatusCode,
                                         ((HttpWebResponse)((WebException)ex).Response).StatusDescription));
                }
                else
                {
                    return(ex.Message);
                }
            }

            LoadSettings(prevNetSettings); // revert

            proxyCredDialog.AlwaysDisplay = false;
            return(String.Empty); // no error message = success
        }
        /// <summary>
        /// Apply the given network settings for use from now on.
        /// </summary>
        /// <remarks>Throws an exception if any settings are invalid.</remarks>
        /// <param name="settings">The NetworkSettings to use.</param>
        public void LoadSettings(NetworkSettings settings)
        {
            this.wiretapServer = new UriBuilder("http", settings.wiretapHost, settings.WiretapPortInt).Uri;

            if (settings.useCustomProxy)
            {
                if (settings.proxyHost == "")
                {
                    WebRequest.DefaultWebProxy = null; // force no proxy
                }
                else
                {
                    WebRequest.DefaultWebProxy = new WebProxy(new UriBuilder("http", settings.proxyHost, settings.ProxyPortInt).Uri);
                }
            }
            else
            {
                WebRequest.DefaultWebProxy = IEProxy;
            }
        }