private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Make sure a recipient is selected to chat with
                if (lstUsers.SelectedItems.Count < 1)
                {
                    MessageBox.Show("You must select who to chat with.");
                    return;
                }

                Chat.Message MSG = new Chat.Message();
                MSG.Sender = Client;
                MSG.MSG    = txtMessage.Text;

                switch (cmbAction.SelectedIndex)
                {
                case 0:
                    MSG.Type = Chat.MGSType.Chat;
                    break;

                case 1:
                    MSG.Type = Chat.MGSType.PowerShell;
                    break;

                case 2:
                    MSG.Type = Chat.MGSType.ComputerInfo;
                    break;
                }


                //Add the selected recipients to the table
                foreach (string user in lstUsers.SelectedItems)
                {
                    Chat.Client c = new Chat.Client();
                    c.Name = user;
                    MSG.Reciptients.Add(c);
                }

                //Update the sender's message history
                txtMessageHistory.Text += Client.Name + " - " + txtMessage.Text + Environment.NewLine;

                byte[] msg = Chat.Serialize(Chat.Action.Message, MSG);

                if (Session.SendMessage(msg) == false)
                {
                    // Show the exception
                    MessageBox.Show(Session.Exception);
                    return;
                }

                //Clear the message box
                txtMessage.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        internal void receivedData(Chat.Packet packet)
        {
            Chat.Clients recipients = new Chat.Clients();

            if (packet != null)
            {
                switch (packet.ID)
                {
                case Chat.Action.Join:

                    // Deserialize the content
                    Chat.Client usr = Chat.Client.Deserialize(packet.Content);
                    ClientName = usr.Name;

                    // Add the user set to receive a full list of user
                    recipients.List.Add(usr);

                    Chat.Clients usrs = new Chat.Clients();
                    foreach (DictionaryEntry c in IPList)
                    {
                        Chat.Client u = new Chat.Client();
                        u.Name = ((Server)c.Value).ClientName;
                        usrs.List.Add(u);
                    }

                    byte[] msg = Chat.Serialize(Chat.Action.ClientList, usrs);

                    //Send the full user list to the user that just signed on
                    Broadcast(msg, recipients);

                    //Rebroadcast the original message to everyone that this user is online
                    Broadcast(Data, null);

                    break;

                case Chat.Action.Message:
                    Chat.Message message = Chat.Message.Deserialize(packet.Content);

                    foreach (Chat.Client recipient in message.Reciptients)
                    {
                        recipients.List.Add(recipient);
                    }

                    //Send the message to the recipients
                    Broadcast(Data, recipients);
                    break;

                default:

                    break;
                }
            }
        }
        public void sessionEnded(string clientIP)
        {
            IPList.Remove(clientIP);

            Chat.Client usr = new Chat.Client();
            usr.Name = ClientName;


            byte[] msg = Chat.Serialize(Chat.Action.Disconnect, usr);

            Broadcast(msg, null);
        }
        public bool JoinClient(Chat.Client user)
        {
            Exception = null;
            bool result = false;

            try
            {
                // Serialize the message
                byte[] msg = Chat.Serialize(Chat.Action.Join, user);

                SendMessage(msg);

                result = true;
            }
            catch (Exception ex)
            {
                Exception = ex.Message;
            }

            return(result);
        }
        //Delegate and subroutine to update the textBox control
        //Friend Delegate Sub delUpdateHistory(ByVal str As String)
        internal void receivedData(Chat.Packet packet)
        {
            switch (packet.ID)
            {
            case Chat.Action.Join:

                Chat.Client usr = Chat.Client.Deserialize(packet.Content);
                lstUsers.Items.Add(usr.Name);

                break;

            case Chat.Action.ClientList:
                lstUsers.Items.Clear();

                Chat.Clients usrs = Chat.Clients.Deserialize(packet.Content);
                foreach (Chat.Client u in usrs.List)
                {
                    if (Client.Name != u.Name)
                    {
                        lstUsers.Items.Add(u.Name);
                    }
                }

                break;

            case Chat.Action.Message:
                //Read the message
                Chat.Message msg = Chat.Message.Deserialize(packet.Content);

                switch (msg.Type)
                {
                case Chat.MGSType.Chat:

                    break;

                case Chat.MGSType.PowerShell:

                    break;

                case Chat.MGSType.ComputerInfo:

                    break;
                }

                //Read the recipients...

                //Update the recipient's message history
                txtMessageHistory.Text += msg.Sender.Name + " - " + msg.MSG + Environment.NewLine;
                break;

            case Chat.Action.Disconnect:
                Chat.Client user = Chat.Client.Deserialize(packet.Content);

                //Remove the user from the listbox
                lstUsers.Items.Remove(user.Name);

                break;

            default:
                break;
            }
        }