private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            if (isInternetup)
            {
                SettingsWindow winsetting = new SettingsWindow();
                winsetting.ShowDialog();

                if (isInternetup)
                {
                    if (_CallOut_CodingService != null)
                    {
                        _CallOut_CodingService.Close();
                        //_CallOut_CodingService = null;
                    }

                    //Or check one of the config file variable for IP address or null
                    if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "")
                    {
                        string          endpointaddress = "net.tcp://" + Properties.Settings.Default.CodingIP + ":8000/CallOut_CodingService/service";
                        EndpointAddress endpointaddr    = new EndpointAddress(new Uri(endpointaddress));
                        _CallOut_CodingService = new ServiceReference1.CallOut_CodingServiceClient(new InstanceContext(this), "NetTcpBinding_CallOut_CodingService", endpointaddr);
                        Log("Open Coding Service proxy");
                        _CallOut_CodingService.Open();
                    }
                }
                else //to catch the return from showdialog
                {
                    ShowMessageBox("Please check the internet connection to continue");
                }
            }
            else
            {
                ShowMessageBox("Please check the internet connection to continue");
            }
        }
        /// <summary>
        /// Report the current network availability.
        /// </summary>

        private void ReportAvailability()
        {
            if (CheckForInternetConnection())
            {
                isInternetup = true;
                //SendOrPostCallback callback =
                //    delegate(object state)
                //    {
                //        this.panelConnection.Visibility = Visibility.Collapsed;
                //    };
                //_uiSyncContext.Post(callback, "Internet Up");
                //Do not have to open another messagebox or proxy as mainwindow had been triggered
                if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "")
                {
                    string          endpointaddress = "net.tcp://" + Properties.Settings.Default.CodingIP + ":8000/CallOut_CodingService/service";
                    EndpointAddress endpointaddr    = new EndpointAddress(new Uri(endpointaddress));
                    _CallOut_CodingService = new ServiceReference1.CallOut_CodingServiceClient(new InstanceContext(this), "NetTcpBinding_CallOut_CodingService", endpointaddr);
                    Log("open new coding service proxy from network change");
                    _CallOut_CodingService.Open();
                }
            }
            else
            {
                isInternetup = false;
                //SendOrPostCallback callback =
                //    delegate(object state)
                //    {
                //        this.panelConnection.Visibility = Visibility.Visible;
                //    };
                //_uiSyncContext.Post(callback, "Internet Down");

                if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "")
                {
                    _CallOut_CodingService.Abort();
                    _CallOut_CodingService = null;
                }
            }
        }
        private void MyWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // Capture the UI synchronization context
            _uiSyncContext = SynchronizationContext.Current;

            //Fill in the IP address from IP config file
            string[] lineOfContents = File.ReadAllLines(@"..\..\..\..\IPConfigFile.txt");
            this.txtCodingSvcIP.Text = lineOfContents[3].Trim();

            //Check whether CodingIP exist
            if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "")
            {
                this.txtCodingSvcIP.Text      = Properties.Settings.Default.CodingIP;
                this.txtCodingSvcIP.IsEnabled = false;

                _isCodingSvcIPSet     = true;
                this.btnSetIP.Content = "Unset IP";

                string          endpointaddress = "net.tcp://" + this.txtCodingSvcIP.Text.Trim() + ":8000/CallOut_CodingService/service";
                EndpointAddress endpointaddr    = new EndpointAddress(new Uri(endpointaddress));
                _CallOut_CodingService = new ServiceReference1.CallOut_CodingServiceClient(new InstanceContext(this), "NetTcpBinding_CallOut_CodingService", endpointaddr);
                Log("open new coding service proxy from setting window");
                _CallOut_CodingService.Open();

                //Connection is UP!!!

                //Fill up the combobox items
                foreach (StationStatus station in _CallOut_CodingService.GetStationStatus())
                {
                    this.comboID.Items.Add(station.Station);
                }

                //Fill the combobox selected text
                this.comboID.Text = Properties.Settings.Default.CurrentID;

                //DataGrid Bind
                _ConsoleLogList.Clear();

                //Load out the console log status from configfile
                if (this.comboID.Text != "")
                {
                    this.comboID.IsEnabled = false;
                    _isStationIDSet        = true;
                    this.btnSetID.Content  = "Unset";

                    if (Properties.Settings.Default.ConsoleLogList != null)
                    {
                        foreach (string log in Properties.Settings.Default.ConsoleLogList)
                        {
                            string[] parts = log.Split(',');
                            //Check is it corresponding stationID
                            if (parts[0] == this.comboID.Text)
                            {
                                ConsoleLog consolelog = new ConsoleLog();
                                consolelog.CodingID     = parts[1];
                                consolelog.AckTimeStamp = parts[2];
                                consolelog.AckFrom      = parts[3];
                                consolelog.AckStatus    = parts[4];
                                _ConsoleLogList.Add(consolelog);
                            }
                        }
                    }
                }
                this.lvConsoleLog.ItemsSource = _ConsoleLogList;
            }
        }
        private void btnSetIP_Click(object sender, RoutedEventArgs e)
        {
            if (isInternetup)
            {
                if (_isCodingSvcIPSet)
                {
                    if (!_isStationIDSet)
                    {
                        //UnSetIP

                        //Clear ComboBox and Console Log
                        this.comboID.Items.Clear();

                        //_ConsoleLogList.Clear();
                        //this.lvConsoleLog.ItemsSource = _ConsoleLogList;

                        //Cut off the proxy / channel
                        _CallOut_CodingService.Close();
                        _CallOut_CodingService = null;

                        //Remove the IP Address in config file
                        Properties.Settings.Default.CodingIP = "";
                        Properties.Settings.Default.Save();

                        this.txtCodingSvcIP.IsEnabled = true;
                        _isCodingSvcIPSet             = false;
                        this.btnSetIP.Content         = "Set IP";
                    }
                    else
                    {
                        MessageBox.Show("Unset Station ID before unset IP address...");
                    }
                }
                else
                {
                    //Set IP

                    //Trigger to create Coding Service Client with the input IP
                    try
                    {
                        string endpointaddress = "net.tcp://" + this.txtCodingSvcIP.Text.Trim() + ":8000/CallOut_CodingService/service";
                        _CallOut_CodingService = new ServiceReference1.CallOut_CodingServiceClient(new InstanceContext(this), "NetTcpBinding_CallOut_CodingService", endpointaddress);
                        Log("Open coding service proxy");
                        _CallOut_CodingService.Open();

                        //Save the IP address that is confirm
                        Properties.Settings.Default.CodingIP = this.txtCodingSvcIP.Text.Trim();
                        Properties.Settings.Default.Save();

                        //Fill up the combobox items
                        foreach (StationStatus station in _CallOut_CodingService.GetStationStatus())
                        {
                            this.comboID.Items.Add(station.Station);
                        }

                        //Fill the combobox selected text
                        this.comboID.Text = Properties.Settings.Default.CurrentID;

                        //DataGrid Bind
                        //_ConsoleLogList.Clear();

                        //Load out the console log status from configfile
                        if (this.comboID.Text != "")
                        {
                            this.comboID.IsEnabled = false;

                            if (Properties.Settings.Default.ConsoleLogList != null)
                            {
                                foreach (string log in Properties.Settings.Default.ConsoleLogList)
                                {
                                    string[] parts = log.Split(',');
                                    //Check is it corresponding stationID
                                    if (parts[0] == this.comboID.Text)
                                    {
                                        ConsoleLog consolelog = new ConsoleLog();
                                        consolelog.CodingID     = parts[1];
                                        consolelog.AckTimeStamp = parts[2];
                                        consolelog.AckFrom      = parts[3];
                                        consolelog.AckStatus    = parts[4];
                                        _ConsoleLogList.Add(consolelog);
                                    }
                                }
                            }
                        }

                        this.lvConsoleLog.ItemsSource = _ConsoleLogList;

                        this.txtCodingSvcIP.IsEnabled = false;
                        _isCodingSvcIPSet             = true;
                        this.btnSetIP.Content         = "Unset IP";
                    }
                    catch (Exception E)
                    {
                        _CallOut_CodingService = null;
                        MessageBox.Show("Invalid IP address...");
                    }
                }
            }
            else
            {
                ShowMessageBox("Please check the internet connection to continue");
            }
        }
        /// <summary>
        /// Report the current network availability.
        /// </summary>

        private void ReportAvailability()
        {
            if (CheckForInternetConnection())
            {
                isInternetup = true;
                //SendOrPostCallback callback =
                //    delegate(object state)
                //    {
                //        this.panelConnection.Visibility = Visibility.Collapsed;
                //    };
                //_uiSyncContext.Post(callback, "Internet Up");

                if (IncidentMsgQueue.Count == 0)
                {
                    beenCutOffBefore = false;
                }
                //open another thread to show message box so it wont hold up the rest of the code
                //ShowMessageBox("Internet connection is up...");
                Log("Internet connection is up...");

                //establish new
                //Or check one of the config file variable for IP address or null (check for codingservice null to determine is offline at setting or main window)
                if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "")
                {
                    string          endpointaddress = "net.tcp://" + Properties.Settings.Default.CodingIP + ":8000/CallOut_CodingService/service";
                    EndpointAddress endpointaddr    = new EndpointAddress(new Uri(endpointaddress));
                    _CallOut_CodingService = new ServiceReference1.CallOut_CodingServiceClient(new InstanceContext(this), "NetTcpBinding_CallOut_CodingService", endpointaddr);
                    Log("open new coding service proxy from network change");
                    _CallOut_CodingService.Open();
                }
            }
            else
            {
                isInternetup     = false;
                beenCutOffBefore = true;
                //open another thread to show message box so it wont hold up the rest of the code
                //ShowMessageBox("Internet connection is down...");
                Log("Internet connection is down...");
                //abort only closes the client. The service instance/session is still alive until instance timeout.
                if (Properties.Settings.Default.CodingIP != null && Properties.Settings.Default.CodingIP != "" && _CallOut_CodingService != null)
                {
                    _CallOut_CodingService.Abort();
                    _CallOut_CodingService = null;
                }
                //Simulate Logout from console ID
                SendOrPostCallback callback =
                    delegate(object state)
                {
                    //this.panelConnection.Visibility = Visibility.Visible;
                    if (counttotal != 0)
                    {
                        this.Title = "Call Out Console (" + countdisplay.ToString() + " of " + counttotal.ToString() + ")";
                    }
                    Properties.Settings.Default.isConnected = false;
                    Properties.Settings.Default.Save();
                    this.btnConnect.IsChecked = false;
                    //Update hidden label
                    lblStatus = "";

                    //this.txtConsoleName.Text = "";
                    //this.txtIncidentAmt.Text = "0";
                    //this.txtAckAmt.Text = "0";
                    //this.txtRejectAmt.Text = "0";
                    //this.txtFailedAmt.Text = "0";

                    IncidentCount = 0;
                    AckCount      = 0;
                    RejectedCount = 0;
                    FailedCount   = 0;
                };

                _uiSyncContext.Post(callback, "Internet Down");
            }
        }