public void StartListening()
        {
            mouse = null;
            keybd = null;
            byte[] bytes = new Byte[1024];
            Console.WriteLine("sono bindato all'indirizzo " + resolve() + "porta " + port);
            form.DoSomething("sono in ascolto all'indirizzo " + resolve());
            ipAddress = IPAddress.Parse(resolve());
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

            listener = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Stream, ProtocolType.Tcp);

            try
            {
                mousethread    = null;
                keybdthread    = null;
                invioClipboard = null;
                avvioServInvio = null;

                listener.Bind(localEndPoint);
                listener.Listen(1);
                terminateGracefully = false;
                while (!terminateGracefully)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Console.WriteLine("la password è " + password);
                    // Program is suspended while waiting for an incoming connection.
                    handler = listener.Accept();
                    SetKeepAlive(handler, 90, 100);
                    //  handler.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);
                    /***************************** CREAZIONE DEI THREAD. ****************************************/
                    terminatecurrentsocketPrinc = false;
                    StateObject state = new StateObject();
                    state.workSocket = handler;
                    if (mousethread == null)
                    {
                        mouse = new MouseUDP();
                        mouse.getServer(this);
                        // mouse.setevent)(autoMouse);
                        mousethread = new Thread(new ThreadStart(mouse.mouserun));
                        mousethread.IsBackground = true; // ricorda che il thread in background viene terminato immediatamente.
                        // al contrario di un thread in primo piano che deve attendere il completamento dell'operazione.
                        // mettendo il thread in background quando si chiude la finestra viene termianto tutto automaticamente.

                        mousethread.Name = "mouse thread";
                        mousethread.Start();
                    }
                    if (keybdthread == null)
                    {
                        keybd = new KeybdTCP();
                        keybd.SetServer(this);
                        keybdthread              = new Thread(new ThreadStart(keybd.keybdrun));
                        keybdthread.Name         = "keybd thread";
                        keybdthread.IsBackground = true;
                        keybdthread.Start();
                        flag = false;
                    }
                    if (avvioServInvio == null)
                    {
                        clp.SetServer(this);
                        clp.form1                   = form;
                        avvioServInvio              = new Thread(new ThreadStart(clp.setnewserver));
                        avvioServInvio.Name         = "Server per invio.";
                        avvioServInvio.IsBackground = true;
                        avvioServInvio.Start();
                    }
                    if (invioClipboard == null)
                    {
                        myclp = new MyClipBoard();
                        myclp.setServer(this);
                        myclp.setForm(form);
                        invioClipboard              = new Thread(new ThreadStart(myclp.receiveDataClipBoard));
                        invioClipboard.Name         = "server ricezione myclipboard";
                        invioClipboard.IsBackground = true;
                        invioClipboard.Start();
                    }
                    /**********************************************************/

                    connected = false;

                    /*  autoMouse.Reset();
                     * autoRecv.Reset();
                     * autoTast.Reset();*/
                    bool flagerrore = false;
                    while (!terminatecurrentsocketPrinc)
                    {
                        Console.WriteLine("sono connesso ");
                        // An incoming connection needs to be processed.
                        PrincreceiveDone.Reset();
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                        PrincreceiveDone.WaitOne();
                    }
                    if (handler != null)
                    {
                        handler.Close();
                    }

                    form.DoSomething("Online in attesa di Host");

                    // chiudo i socket per far tornare bene le callback
                }
                if (listener != null)
                {
                    listener.Close(); listener = null;
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                if (handler != null)
                {
                    handler.Close(); handler = null;
                }
                if (listener != null)
                {
                    listener.Close(); listener = null;
                }
                Console.WriteLine("errore nel socket uccido tutti i thread e ricomincio.");
                if (mousethread != null)
                {
                    mouse.closeSocket();
                }
                if (keybdthread != null)
                {
                    keybd.closeSocket();
                }
                if (avvioServInvio != null)
                {
                    clp.closeSocket();
                }
                if (invioClipboard != null)
                {
                    myclp.closeSocket();
                }
                mousethread    = null;
                keybdthread    = null;
                invioClipboard = null;
                avvioServInvio = null;
                if (form != null)
                {
                    form.DoSomething2("Offline");
                }
            }
        }
        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();
            }
        }