public void sendData()
        {
            byte[] msg      = new byte[1024];
            byte[] buffer   = new byte[1024 * 1024];
            byte[] conferma = new byte[1024];

            try
            {
                form1.ResizeIcon("invio in corso");
                if (this.valore == 1)
                {
                    for (int i = 0; i < filename.Count; i++)
                    {
                        if (!Directory.Exists(filename[i]))
                        {
                            int result = this.SendFile(filename[i]);
                            if (result == -2)
                            {
                                // clipsocket.Send(Encoding.ASCII.GetBytes("-ERR\r\n"));

                                Console.WriteLine("errore nella send data.");
                                filename.RemoveRange(0, filename.Count);
                                return;
                            }
                            else if (result == -1)
                            {
                                Console.WriteLine("errore nella send data.");
                                filename.RemoveRange(0, filename.Count);
                                return;
                            }
                            clipsocket.Receive(conferma);
                        }
                        else
                        {
                            if (this.SendDir(filename[i]) < 0)
                            {
                                Console.WriteLine("errore nella send dir.");
                                filename.RemoveRange(0, filename.Count);
                                return;
                            }
                        }
                    }
                    Console.WriteLine("Il client ha ricevuto il file");
                    msg = Encoding.ASCII.GetBytes("END");
                    clipsocket.Send(msg);
                    Console.WriteLine("Invio END al client");

                    filename.RemoveRange(0, filename.Count);
                }


                if (this.valore == 0)
                {
                    clipsocket.Send(Encoding.ASCII.GetBytes("T"));
                    Console.WriteLine("Il testo selezionato è:  " + textselected);
                    Encoding enc = Encoding.GetEncoding("iso-8859-1");
                    msg = enc.GetBytes(textselected);
                    clipsocket.Send(msg);
                }

                if (this.valore == 2)
                {
                    clipsocket.Send(Encoding.ASCII.GetBytes("I"));
                    SendVarData(clipsocket, m);
                }
            }
            catch (SocketException ex) {
                Console.WriteLine(ex.ToString());
                if (backgroundThread != null)
                {
                    backgroundThread.Abort();
                    backgroundThread.Join();
                }
            }
        }
        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();
            }
        }