public void RunSecondForm()
 {
     active = new ShowActiveServer(form);
     Application.Run(active);
 }
        public void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;

                // Read data from the remote device.
                int bytesRead = state.workSocket.EndReceive(ar);



                data = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
                if (bytesRead == 0)
                {
                    if (active != null)
                    {
                        active.Nascondi();
                    }
                    if (handler != null)
                    {
                        handler.Close();
                    }

                    Console.WriteLine("il client ha chiuso la connessione");

                    terminatecurrentsocketPrinc = true;
                    PrincreceiveDone.Set();

                    return;
                }
                if (data.IndexOf("PASS") > -1)
                {
                    remoteIpEndPoint = handler.RemoteEndPoint as IPEndPoint;
                    Console.WriteLine(data.Substring(4, data.Length - 9));
                    string pass = this.Encrypt(password);
                    Console.WriteLine("la password del server รจ {0}", pass);
                    if (pass == data.Substring(4, data.Length - 9))
                    {
                        // MessageBox.Show("Nuovo host collegato.","Attenzione");
                        msg = Encoding.ASCII.GetBytes("+OK\r\n");
                        form.DoSomething("I am connected to " + remoteIpEndPoint.Address + " on port number " + remoteIpEndPoint.Port);
                        handler.Send(msg);
                    }
                    else
                    {
                        msg = Encoding.ASCII.GetBytes("-ERR\r\n");
                        form.DoSomething("tentativo di autenticazione dall'hos " + remoteIpEndPoint.Address + " fallito");
                        handler.Send(msg);
                    }
                }

                if (data.IndexOf("-ERR\r\n") > -1)
                {
                    clp.err = true;
                }

                if (data.IndexOf("CONTR") > -1)
                {
                    msg = Encoding.ASCII.GetBytes("+OK\r\n");
                    form.DoSomething("attualmente in uso da host" + remoteIpEndPoint.Address);
                    handler.Send(msg);

                    int bytesRec = handler.Receive(state.buffer);
                    coordinateCLient = Encoding.ASCII.GetString(state.buffer, 0, bytesRec);
                    Console.WriteLine("le coordinate sono " + coordinateCLient);
                    connected = true;
                    if (disegna == null)
                    {
                        disegna = new Thread(new ThreadStart(RunSecondForm));
                        disegna.Start();
                    }
                    else
                    {
                        if (active != null)
                        {
                            active.Mostra();
                        }
                    }

                    //mouse.mouserun();
                }

                // Console.WriteLine("********************" + data);

                if (data.IndexOf("SVEGLIA") > -1)
                {
                    if (disegna == null)
                    {
                        disegna = new Thread(new ThreadStart(RunSecondForm));
                        disegna.Start();
                    }
                    else
                    {
                        if (active != null)
                        {
                            active.Mostra();
                        }
                    }
                    form.DoSomething("MI sono svegliato sono il server corrente");
                    connected = true;
                    form.ResizeIcon("attualmente in uso da Host " + remoteIpEndPoint.Address);
                }

                if (data.IndexOf("DORMI") > -1)
                {
                    form.DoSomething("MI sono addormentato non  sono il server corrente");
                    if (active != null)
                    {
                        active.Nascondi();
                    }
                    connected = false;
                    // form.ResizeIcon("Server non in uso . ");
                }

                PrincreceiveDone.Set();
            }
            catch (Exception ex)
            {
                if (handler != null)
                {
                    handler.Close(); handler = null;
                }

                if (keybd.keybsock != null)
                {
                    keybd.keybsock.Close(); keybd.keybsock = null;
                }
                if (clp.clipsocket != null)
                {
                    clp.clipsocket.Close(); clp.clipsocket = null;
                }
                if (myclp.sockClipBoard != null)
                {
                    myclp.sockClipBoard.Close(); myclp.sockClipBoard = null;
                }
                if (active != null)
                {
                    active.chiudi(); active = null; disegna = null;
                }
                terminatecurrentsocketPrinc = true;
                PrincreceiveDone.Set();
            }
        }