void Toggle_Button_Click(object sender, RoutedEventArgs e, TCPSocket client, string command)
 {
     if (client.connected == true)
     {
         if (command == "Ign")
         {
             if (IgnTOG_Button.Content.Equals("Ignition OFF"))
             {
                 client.sendMessage("2");
             }
             else
             {
                 client.sendMessage("3");
             }
         }
         else if (command == "Val")
         {
             if (ValTOG_Button.Content.Equals("Valve Closed"))
             {
                 client.sendMessage("4");
             }
             else
             {
                 client.sendMessage("5");
             }
         }
     }
     else
     {
         MessageBox.Show("No Connection Established", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 void Button_Click(object sender, RoutedEventArgs e, TCPSocket client, string command)
 {
     if (client.connected == true)
     {
         client.sendMessage(command);
     }
     else
     {
         MessageBox.Show("No Connection Established", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 void Confirm_Button_Click(object sender, RoutedEventArgs e, TCPSocket client, string message, string command)
 {
     if (client.connected == true)
     {
         MessageBoxResult result = MessageBox.Show("Are you sure you wish to " + message + "?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
         if (result == MessageBoxResult.Yes)
         {
             client.sendMessage(command);
         }
     }
     else
     {
         MessageBox.Show("No Connection Established", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
 void Connect_Button_Click(object sender, RoutedEventArgs e, TCPSocket client, DispatcherTimer timer)
 {
     if (client.connected == false)
     {
         MessageBoxResult result = MessageBox.Show("Are you sure you wish to Connect?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
         if (result == MessageBoxResult.Yes)
         {
             client.init();
             if (client.connected == true)
             {
                 client.sendMessage("Hello");
                 timer.Start();
                 MessageBox.Show("Connection Established", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
             }
         }
     }
     else
     {
         MessageBox.Show("Connection Already Established", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
     }
 }
        private void timer_Tick(object sender, EventArgs e)
        {
            int numofMsg = 3;

            if (client.delay == false)
            {
                if (client.connected == true)
                {
                    Connect_Button.Background = Brushes.Green;
                    long startTime = stopWatch.ElapsedMilliseconds;
                    stopWatch.Start();
                    client.sendMessage("0");
                    string buffer = client.recvMessage();
                    stopWatch.Stop();
                    long timeDiff = stopWatch.ElapsedMilliseconds - startTime;
                    Latency.Text = timeDiff.ToString();
                    string[] temp = buffer.Split(';');
                    if (temp.Length > 1)
                    {
                        if (temp[0] != "0" && temp[0] != lastMsg)
                        {
                            for (int i = 0; i < numofMsg; i++)
                            {
                                if (msgArray[i] == "")
                                {
                                    msgArray[i] = temp[0];
                                    lastMsg     = temp[0];
                                    break;
                                }
                                else if (msgArray[i] != "" && i == numofMsg - 1)
                                {
                                    for (int j = 0; j < numofMsg - 1; j++)
                                    {
                                        msgArray[j] = msgArray[j + 1];
                                    }
                                    msgArray[numofMsg - 1] = temp[0];
                                    lastMsg = temp[0];
                                }
                            }
                            msg0.Text = msgArray[0];
                            msg1.Text = msgArray[1];
                            msg2.Text = msgArray[2];
                            //msg4.Text = msgArray[4];
                        }
                        if (temp[1] == "1")
                        {
                            IgnTOG_Button.Background = Brushes.Green;
                            IgnTOG_Button.Content    = "Ignition ON";
                        }
                        else
                        {
                            IgnTOG_Button.Background = Brushes.Red;
                            IgnTOG_Button.Content    = "Ignition OFF";
                        }
                        if (temp[2] == "1")
                        {
                            ValTOG_Button.Background = Brushes.Green;
                            ValTOG_Button.Content    = "Valve Open";
                        }
                        else
                        {
                            ValTOG_Button.Background = Brushes.Red;
                            ValTOG_Button.Content    = "Valve Closed";
                        }
                        if (temp[3] == "1")
                        {
                            Burn.Text = "Burn Wire Connected";
                        }
                        else
                        {
                            Burn.Text = "Burn Wire Cut";
                        }
                        Pre.Text   = temp[4];
                        Error.Text = temp[5];
                    }
                }
                else
                {
                    Connect_Button.Background = Brushes.LightGray;
                    IgnTOG_Button.Background  = Brushes.LightGray;
                    ValTOG_Button.Background  = Brushes.LightGray;
                    msgArray[0]  = "";
                    msgArray[1]  = "";
                    msgArray[2]  = "";
                    msg0.Text    = "";
                    msg1.Text    = "";
                    msg2.Text    = "";
                    Burn.Text    = "";
                    Pre.Text     = "";
                    Error.Text   = "";
                    Latency.Text = "";
                }
            }
            else
            {
                client.delay = false;
            }
        }