//**********************************************************************//
        // Button cluck for the car arrived button.  All it does is send the    //
        // string "Car" to the server.                                          //
        //**********************************************************************//
        private void buttonCarArrived_Click(object sender, EventArgs e)
        {
            // Create a message to send to server
            MessageProtocol sendCarMessage = new MessageProtocol(uniqueIDNumber, "CarSent", "-");

            sendString(sendCarMessage.GetMessageToSend(), textBoxLightIP.Text);
        }
 //*********************************************************************//
 // Change the status of the lights.                                    //
 //*********************************************************************//
 private void ChangeLights(MessageProtocol command)
 {
     if (command == null)
     {
         return;                     // Nothing to do.
     }
     // Determine which light to turn on/off
     if (command.GetActionValue().Equals("Red_On"))
     {
         labelRed.Visible = true;
     }
     if (command.GetActionValue().Equals("Amber_On"))
     {
         labelAmber.Visible = true;
     }
     if (command.GetActionValue().Equals("Green_On"))
     {
         labelGreen.Visible = true;
     }
     if (command.GetActionValue().Equals("Red_Off"))
     {
         labelRed.Visible = false;
     }
     if (command.GetActionValue().Equals("Amber_Off"))
     {
         labelAmber.Visible = false;
     }
     if (command.GetActionValue().Equals("Green_Off"))
     {
         labelGreen.Visible = false;
     }
 }
 private void timer_Tick(object sender, EventArgs e)
 {
     // If the client is connected, send a car every once in a while
     if (uniqueIDNumber != null && randomGen.Next(4) == 0)
     {
         MessageProtocol sendCarMessage = new MessageProtocol(uniqueIDNumber, "CarSent", "-");
         sendString(sendCarMessage.GetMessageToSend(), textBoxLightIP.Text);
     }
 }
        //*********************************************************************//
        // Form closing.  If the connection thread was ever created then kill  //
        // it off.                                                             //
        //*********************************************************************//
        private void FormTrafficLight_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageProtocol disconncetionMessage = new MessageProtocol(uniqueIDNumber, "ClientDisconnecting", "-");

            sendString(disconncetionMessage.GetMessageToSend(), textBoxLightIP.Text);

            if (threadConnection != null)
            {
                threadConnection.StopThread();
            }
        }
        private void connectServerButton_Click(object sender, EventArgs e)
        {
            // Create message to send connection request
            MessageProtocol connectMessage = new MessageProtocol("#", "ConnectionRequest", "-");

            if (ValidateIPAddress() == true)
            {
                waitingToConnect = true;

                // Send string requesting the unique ID assigned by the server
                sendString(connectMessage.GetMessageToSend(), textBoxLightIP.Text);
            }
        }
        //*********************************************************************//
        // Message was posted back to us.  This is to get over the C# threading//
        // rules whereby we can only touch the UI components from the thread   //
        // that created them, which is the form's main thread.                 //
        //*********************************************************************//
        public void MessageReceived(Object received)
        {
            String message = (String)received;

            listBoxOutput.Items.Add(DateTime.Now.ToString("h:mm:ss tt") + "  " + message);

            // Fit the received message to message protocol
            MessageProtocol receivedMessage = new MessageProtocol(message);

            // If the message is for this client and it is a light change request
            if (receivedMessage.GetID() == uniqueIDNumber && receivedMessage.GetActionType().Equals("ChangeLight"))
            {
                ChangeLights(receivedMessage);
            }
            // If the connection request is accepted and this client is waiting to connect
            else if (receivedMessage.GetActionType().Equals("ConnectionRequestAccepted") && waitingToConnect == true)
            {
                // Save the assigned ID number by the server
                uniqueIDNumber     = receivedMessage.GetActionValue();
                idNumberLabel.Text = uniqueIDNumber;

                // Enable or disable some parts of UI
                buttonCarArrived.Enabled    = true;
                randomSendCheckBox.Enabled  = true;
                connectServerButton.Enabled = false;
                textBoxLightIP.Enabled      = false;
                waitingToConnect            = false;
            }
            // If the client receives message to disconnect from server
            else if (receivedMessage.GetActionType().Equals("ClientDisconnect"))
            {
                // Turn off the lights
                labelRed.Visible   = false;
                labelAmber.Visible = false;
                labelGreen.Visible = false;

                // Reset ID number assigned to this client
                uniqueIDNumber     = null;
                idNumberLabel.Text = "";

                // Enable or disable parts of UI
                buttonCarArrived.Enabled    = false;
                connectServerButton.Enabled = true;
                textBoxLightIP.Enabled      = true;
            }
        }