private void deleteClick(object sender, EventArgs e) // remove client from list, this function is triggered from "DELETE" button
        {
            if (connected)
            {
                connected = false;
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Lost connection" + "  (" + index + ")" + "\n");
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + "  (" + index + ")" + "\n");
                AsynchronousServer.clients.Remove(this.key);
                client.Shutdown(SocketShutdown.Both);
                client.Close();
                TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel));
            }
            else
            {
                AsynchronousServer.clients.Remove(this.key);
                TestFormCotrolHelper.ControlInvike(AsynchronousServer.list, () => AsynchronousServer.list.Controls.Remove(itemPanel));
                AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Deleted" + "  (" + index + ")" + "\n");
            }
            AsynchronousServer.StoreData();

            Console.WriteLine("My Index: " + this.index);
            foreach (KeyValuePair <string, Client> item in AsynchronousServer.clients) //move all items below this with 25 units upper, for not leaving blank space in list
            {
                Console.WriteLine("item: " + item.Value.index);
                if (item.Value.index > this.index)
                {
                    item.Value.itemPanel.Location = new System.Drawing.Point(0, item.Value.itemPanel.Location.Y - 25);
                }
            }
        }
 //disconnect all clients from server, and close server if opened
 private void disconnect2_Click(object sender, EventArgs e)
 {
     if (AsynchronousServer.runningServer)
     {
         AsynchronousServer.close();
     }
     else
     {
         showAlert("You are not connected to any server");
     }
 }
 public Client(string key, Socket client, int count)
 {
     this.key    = key;
     this.client = client;
     this.index  = count;
     FillList();
     if (client != null)   //if client is created from server.accept() result, then start listen thread, otherwise means client is created without socket connection, from list of all connected/disconnected clients
     {
         AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "New client connected: " + client.RemoteEndPoint.ToString() + "  (" + index + ")" + "\n");
         instanceClient();
     }
 }
 //send message from server panel
 private void send2_Click(object sender, EventArgs e)
 {
     if (AsynchronousServer.clients.Count == 0)
     {
         showAlert("No client is connected to server");
     }
     else
     {
         String data = this.input2.Text;
         AsynchronousServer.Send(data, -1);
         this.input2.Text = "";
     }
 }
        //open server panel
        private void button2_Click(object sender, EventArgs e)
        {
            clientMode = false;
            this.Controls.Remove(panel1);
            this.Controls.Add(panel2);
            AsynchronousServer.console          = this.serverConsole; //link forms for static usage
            AsynchronousServer.consoleContainer = this.panel6;
            AsynchronousServer.list             = this.panel5;        //link list of all connected clients

            if (!AsynchronousServer.runningServer)
            {
                this.serverConsole.Text = "";
                AsynchronousServer.StartListening();   //autostarts server
            }
        }
        public void instanceClient()
        {
            this.connected           = true;
            itemStatus.ImageLocation = "green.bmp"; //set connected image in clients list

            chatThread = new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                while (connected)
                {
                    try
                    {
                        int receivedBytes = client.Receive(bytes);
                        //get message from connected client in bytes buffer
                        if (receivedBytes == 0) //if not bytes received means client lost connection
                        {
                            AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): Lost connection" + "  (" + index + ")" + "\n");
                            client.Shutdown(SocketShutdown.Both);
                            client.Close();
                            connected = false;
                            itemStatus.ImageLocation = "red.bmp";//set disconnected image in clients list
                        }
                        else //put buffer bytes in string
                        {
                            response = Encoding.ASCII.GetString(bytes, 0, receivedBytes);
                            AsynchronousServer.ServerSetControlPropertyThreadSafe(AsynchronousServer.console, "Text", AsynchronousServer.console.Text + "Client " + index + " (" + key + "): " + response);
                            Console.WriteLine("Console height: " + AsynchronousServer.console.Size.Height);
                            AsynchronousServer.Send(response, index);
                        }
                    }
                    catch (System.Net.Sockets.SocketException e)
                    {
                        connected = false;
                        connected = false;
                        itemStatus.ImageLocation = "red.bmp";//set disconnected image in clients list
                    }
                    Thread.Sleep(500);
                }
            });

            chatThread.Start();
        }
 //connect server if not already
 private void connect2(object sender, EventArgs e)
 {
     if (AsynchronousServer.runningServer)
     {
         showAlert("Server already running");
     }
     else
     {
         String data = this.textBox2.Text;
         if (data.Length > 0)
         {
             //get data from input box, and start server
             this.clientMode         = false;
             AsynchronousServer.port = Int32.Parse(data);
             AsynchronousServer.StartListening();
         }
         else
         {
             showAlert("Bad adress, only allowed ip:port combination");
         }
     }
 }
 //clear server console
 private void button4_Click(object sender, EventArgs e)
 {
     AsynchronousServer.ServerSetControlPropertyThreadSafe(this.serverConsole, "Text", "");
 }