/// <summary>
        /// Brings down each telescope instance and exits from the GUI cleanly.
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            logger.Info("Shut Down Telescope Button Clicked");
            if (MainControlRoomController != null && MainControlRoomController.RequestToKillWeatherMonitoringRoutine())
            {
                logger.Info("Successfully shut down weather monitoring routine.");
            }
            else
            {
                logger.Info("ERROR shutting down weather monitoring routine!");
            }

            // Loop through the list of telescope controllers and call their respective bring down sequences.
            for (int i = 0; i < ProgramRTControllerList.Count; i++)
            {
                if (MainControlRoomController.RemoveRadioTelescopeControllerAt(0, false))
                {
                    logger.Info("Successfully brought down RT controller at index " + i.ToString());
                }
                else
                {
                    logger.Info("ERROR killing RT controller at index " + i.ToString());
                }

                ProgramRTControllerList[0].RadioTelescope.SpectraCyberController.BringDown();
                ProgramRTControllerList[0].RadioTelescope.PLCDriver.Bring_down();
                ProgramPLCDriverList[0].RequestStopAsyncAcceptingClientsAndJoin();
            }

            // End logging
            logger.Info("<--------------- Control Room Application Terminated --------------->");
            Environment.Exit(0);
        }
        /// <summary>
        /// Eventhandler for the start button on the main GUI form. This method creates and
        /// initializes the configuration that is specified on the main GUI form if the correct
        /// fields are populated.
        /// </summary>
        /// <param name="sender"> Object specifying the sender of this Event. </param>
        /// <param name="e"> The eventargs from the button being clicked on the GUI. </param>
        private void button1_Click(object sender, EventArgs e)
        {
            logger.Info("Start Telescope Button Clicked");
            if (txtPLCPort.Text != null &&
                txtPLCIP.Text != null &&
                comboBox1.SelectedIndex > -1)
            {
                current_rt_id++;
                AbstractPLCDriver       APLCDriver = BuildPLCDriver();
                AbstractMicrocontroller ctrler     = build_CTRL();
                ctrler.BringUp();
                AbstractEncoderReader encoder         = build_encoder(APLCDriver);
                RadioTelescope        ARadioTelescope = BuildRT(APLCDriver, ctrler, encoder);


                // Add the RT/PLC driver pair and the RT controller to their respective lists
                AbstractRTDriverPairList.Add(new KeyValuePair <RadioTelescope, AbstractPLCDriver>(ARadioTelescope, APLCDriver));
                ProgramRTControllerList.Add(new RadioTelescopeController(AbstractRTDriverPairList[current_rt_id - 1].Key));
                ProgramPLCDriverList.Add(APLCDriver);

                if (checkBox1.Checked)
                {
                    logger.Info("Populating Local Database");
                    DatabaseOperations.PopulateLocalDatabase(current_rt_id);
                    Console.WriteLine(DatabaseOperations.GetNextAppointment(current_rt_id).StartTime.ToString());
                    logger.Info("Disabling ManualControl and FreeControl");
                    ManualControl.Enabled = false;
                    FreeControl.Enabled   = false;
                }
                else
                {
                    logger.Info("Enabling ManualControl and FreeControl");
                    ManualControl.Enabled = true;
                    FreeControl.Enabled   = true;
                }

                // If the main control room controller hasn't been initialized, initialize it.
                if (MainControlRoomController == null)
                {
                    logger.Info("Initializing ControlRoomController");
                    MainControlRoomController = new ControlRoomController(new ControlRoom(BuildWeatherStation()));
                }

                // Start plc server and attempt to connect to it.
                logger.Info("Starting plc server and attempting to connect to it");
                ProgramPLCDriverList[current_rt_id - 1].StartAsyncAcceptingClients();
//ProgramRTControllerList[current_rt_id - 1].RadioTelescope.PLCClient.ConnectToServer();//////####################################

                logger.Info("Adding RadioTelescope Controller");
                MainControlRoomController.AddRadioTelescopeController(ProgramRTControllerList[current_rt_id - 1]);

                logger.Info("Starting Weather Monitoring Routine");
                MainControlRoomController.StartWeatherMonitoringRoutine();

                // Start RT controller's threaded management
                logger.Info("Starting RT controller's threaded management");
                RadioTelescopeControllerManagementThread ManagementThread = MainControlRoomController.ControlRoom.RTControllerManagementThreads[current_rt_id - 1];
                int RT_ID = ManagementThread.RadioTelescopeID;
                List <Appointment> AllAppointments = DatabaseOperations.GetListOfAppointmentsForRadioTelescope(RT_ID);

                logger.Info("Attempting to queue " + AllAppointments.Count.ToString() + " appointments for RT with ID " + RT_ID.ToString());
                foreach (Appointment appt in AllAppointments)
                {
                    logger.Info("\t[" + appt.Id + "] " + appt.StartTime.ToString() + " -> " + appt.EndTime.ToString());
                }

                if (ManagementThread.Start())
                {
                    logger.Info("Successfully started RT controller management thread [" + RT_ID.ToString() + "]");

                    if (APLCDriver is ProductionPLCDriver)
                    {
                        ProgramRTControllerList[current_rt_id - 1].ConfigureRadioTelescope(500, 500, 0, 0);
                    }
                }
                else
                {
                    logger.Info("ERROR starting RT controller management thread [" + RT_ID.ToString() + "]");
                }

                AddConfigurationToDataGrid();


                /*
                 * Console.WriteLine("at microtherad start");
                 * MicroctrlServerThread = new Thread(new ThreadStart(ControlRoomApplication.Controllers.BlkHeadUcontroler.MicroControlerControler.AsynchronousSocketListener.BringUp));
                 * MicroctrlServerThread.Start();
                 * //ControlRoomApplication.Controllers.BlkHeadUcontroler.MicroControlerControler.AsynchronousSocketListener.BringUp();
                 */
            }
        }