public void CloseConnections()
        {
            if (wisetelescope != null)
            {
                if (wisetelescope.Connected)
                {
                    wisetelescope.Connected = false;
                }
                wisetelescope.Dispose();
                wisetelescope = null;
            }

            if (wisedome != null)
            {
                if (wisedome.Connected)
                {
                    wisedome.Connected = false;
                }
                wisedome.Dispose();
                wisedome = null;
            }

            if (wisesafetooperate != null)
            {
                if (wisesafetooperate.Connected)
                {
                    wisesafetooperate.Connected = false;
                }
                wisesafetooperate.Dispose();
                wisesafetooperate = null;
            }
            connected = false;
        }
 private void buttonConnect_Click(object sender, EventArgs e)
 {
     if (IsConnected)
     {
         _driver.Connected  = false;
         readButton.Enabled = false;
     }
     else
     {
         _driver = new DriverAccess.SafetyMonitor(Properties.Settings.Default.DriverId)
         {
             Connected = true
         };
         readButton.Enabled = true;
     }
     SetUIState();
 }
        public void CheckConnections()
        {
            using (var client = new WebClient())
            {
                try
                {
                    UpdateCheckingStatus("connecting ASCOM server");
                    DateTime start = DateTime.Now;

                    client.DownloadDataAsync(new Uri(Const.RESTServer.top + "concurrency")); // GET to http://www.xxx.yyy.zzz/server/v1/concurrency
                    while (client.IsBusy)
                    {
                        if (DateTime.Now.Subtract(start).TotalMilliseconds > 500)
                        {
                            client.CancelAsync();
                            Log("Connecting ASCOM server timed out");
                            connected = false;
                            return;
                        }
                        Application.DoEvents();
                    }
                    connected = true;
                } catch (Exception ex)
                {
                    Log(string.Format("CheckConnections:Server:Exception: {0}", ex.InnerException == null ? ex.Message : ex.InnerException.Message));
                    connected = false;
                    return;
                }
            }

            try
            {
                if (wisetelescope == null)
                {
                    wisetelescope = new DriverAccess.Telescope("ASCOM.Remote1.Telescope");
                }
                if (!wisetelescope.Connected)
                {
                    wisetelescope.Connected = true;
                    while (wisetelescope.Connected == false)
                    {
                        Log("Waiting for the \"Telescope\" client to connect ...", 5);
                        Application.DoEvents();
                    }
                }

                if (wisesafetooperate == null)
                {
                    wisesafetooperate = new DriverAccess.SafetyMonitor("ASCOM.Remote1.SafetyMonitor");      // Must match ASCOM Remote Server Setup
                }
                if (!wisesafetooperate.Connected)
                {
                    wisesafetooperate.Connected = true;
                    while (!wisesafetooperate.Connected)
                    {
                        Log("Waiting for the \"SafeToOperate\" client to connect ...", 5);
                        Application.DoEvents();
                    }
                }

                if (wisedome == null)
                {
                    wisedome = new DriverAccess.Dome("ASCOM.Remote1.Dome");
                }
                if (!wisedome.Connected)
                {
                    wisedome.Connected = true;
                    while (!wisedome.Connected)
                    {
                        Log("Waiting for the \"Dome\" client to connect", 5);
                        Application.DoEvents();
                    }
                }

                if (!buttonProjector.Enabled)
                {
                    buttonProjector.Enabled = true;
                }

                connected = true;
            }
            catch (Exception ex)
            {
                Log(string.Format("CheckConnections:Exception: {0}", ex.InnerException == null ? ex.Message : ex.InnerException.Message));
                connected = false;
            }
        }