public void RunConnectionTest()
        {
            // Are we already running a test?
            if (this.isTestingConnection)
            {
                return;
            }

            // If not, test connection
            this.isTestingConnection = true;

            ConnectivityTestResults results;
            bool isConnected = ConnetivityTest.Check(settings.App["ConnectionCheck"], out results);

            this.connectivityStatus = results;

            // Did we pass the test?
            if (results.HasPassedTest)
            {
                this.SetConnectionStatus(settings.ConnectionStatus["Connected"]);
            }

            // Ok, we have no internet connection, so just stop here.
            else if (!results.HasInternet)
            {
                this.SetConnectionStatus(settings.ConnectionStatus["NoInternet"]);
            }

            // What if we have internet, but hit a login page?
            else if (results.HasRedirected)
            {
                this.SetConnectionStatus(settings.ConnectionStatus["RequestLogin"]);

                if (this.settings.SavedNetworks.ContainsKey(results.ResponseURL))
                {
                    AppSettings.SavedNetwork network = this.settings.SavedNetworks[results.ResponseURL];
                    this.RunLogin(results.ResponseURL, network.Username, network.Password);
                }
                else
                {
                    loginScreen       = new LoginCreate();
                    loginScreen.Owner = this;
                    loginScreen.ShowDialog();

                    if (loginScreen.Username != null && loginScreen.Username.Length > 0)
                    {
                        this.RunLogin(results.ResponseURL, loginScreen.Username, loginScreen.Password);
                    }
                }
            }

            // Finally, allow tests to run again
            this.isTestingConnection = false;
        }
        public void RunLogin(string url, string username, string password)
        {
            this.SetConnectionStatus(settings.ConnectionStatus["ConnectLogin"]);

            new Thread(() =>
            {
                try
                {
                    // Run login function
                    LoginPage login = LoginPage.Load(url);
                    login.Login(username, password);

                    // After running the login function, send a connection test to our UI thread
                    this.CallUI(new ThreadStart(delegate
                    {
                        this.RunConnectionTest();

                        // If login has worked...
                        if (this.connectivityStatus.HasPassedTest)
                        {
                            // Save current settings
                            AppSettings.SavedNetwork network = new AppSettings.SavedNetwork();
                            network.Username = username;
                            network.Password = password;

                            // Save current login-password combo with this login URL
                            if (!this.settings.SavedNetworks.ContainsKey(url))
                            {
                                this.settings.SavedNetworks.Add(url, network);
                            }

                            this.settings.Save();
                        }

                        // If didn't and we were connected, unsave it
                        else if (this.connectivityStatus.HasInternet && this.settings.SavedNetworks.ContainsKey(url))
                        {
                            this.settings.SavedNetworks.Remove(url);
                        }
                    }));
                }
                catch
                {
                    // If some error happens while logging in, update UI
                    this.CallUI(new ThreadStart(delegate
                    {
                        this.SetConnectionStatus(settings.ConnectionStatus["Error"]);
                    }));
                }
            }).Start();
        }