private void ReceiveCallbackFile(IAsyncResult ar)
        {
            // Retrieve the state object and the client socket
            // from the asynchronous state object.
            StateObject state  = (StateObject)ar.AsyncState;
            Socket      client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = -1;

            try
            {
                bytesRead = client.EndReceive(ar);
            }
            catch (Exception)
            {
                bytesRead = -1;
            }


            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                string dateiName = state.dateiName;
                //long dateiGroesse = state.dateiGroesse;
                try {
                    //Invoke((MethodInvoker)delegate {
                    //    dateiName = filesView2.SelectedItems[0].SubItems[0].Text;
                    //    dateiGroesse = filesView2.SelectedItems[0].SubItems[1].Text;
                    //});
                    string freigabeOrdner   = pfadTextBox.Text;
                    long   dateigroesseLong = state.dateiGroesse;
                    if (dateigroesseLong > 65535)
                    {
                        DirectoryInfo directoryInfo = new DirectoryInfo(freigabeOrdner);
                        FileInfo[]    fileInfoArray = directoryInfo.GetFiles();
                        foreach (FileInfo item in fileInfoArray)
                        {
                            if (item.Name.Equals(dateiName))
                            {
                                long fortschrittInProzent = (((item.Length + bytesRead) * 100) / dateigroesseLong);
                                Invoke((MethodInvoker) delegate {
                                    ListViewItem.ListViewSubItem newItem = new ListViewItem.ListViewSubItem();
                                    newItem.Text = Convert.ToString(fortschrittInProzent) + " %";
                                    foreach (ListViewItem listItem in filesView2.Items)
                                    {
                                        if (listItem.Text.Equals(dateiName))
                                        {
                                            if (listItem.SubItems.Count == 2)
                                            {
                                                listItem.SubItems.Add(newItem);
                                            }
                                            else if (listItem.SubItems.Count == 3)
                                            {
                                                listItem.SubItems[2] = newItem;
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    }
                    else
                    {
                        Invoke((MethodInvoker) delegate {
                            ListViewItem.ListViewSubItem newItem = new ListViewItem.ListViewSubItem();
                            newItem.Text = "100 %";
                            foreach (ListViewItem listItem in filesView2.Items)
                            {
                                if (listItem.Text.Equals(dateiName))
                                {
                                    if (listItem.SubItems.Count == 2)
                                    {
                                        listItem.SubItems.Add(newItem);
                                    }
                                    else if (listItem.SubItems.Count == 3)
                                    {
                                        listItem.SubItems[2] = newItem;
                                    }
                                }
                            }
                        });
                    }


                    semaphoreDateiSpeichern.WaitOne(10);
                    FileStream fileStream = File.Open(freigabeOrdner + "\\" + dateiName, FileMode.Append, FileAccess.Write, FileShare.None);
                    fileStream.Write(state.buffer, 0, bytesRead);
                    fileStream.Close();
                    semaphoreDateiSpeichern.Release();
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }


                //Console.WriteLine(freigabeOrdner + "\\" + dateiName +" gespeichert");
                string response = state.sb.ToString();
            }
            try {
                //client.BeginSend(stateFile.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(AsynchronousFileSendCallback), state);
                //Thread.Sleep(100);
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackFile), state);
            } catch {
            }
        }
        private void chatButton_Click(object sender, EventArgs e)
        {
            string NameDesAusgewaehltenPeers = "";

            try{
                NameDesAusgewaehltenPeers = peersListe.SelectedItems[0].SubItems[1].Text;
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }

            if (IpOfSelectedPeer.Equals("") || NameDesAusgewaehltenPeers.Equals(""))
            {
                MessageBox.Show("Es wurde kein Peer ausgewählt...", "Achtung", MessageBoxButtons.OK);
                return;
            }



            bool        gefunden  = false;
            StateObject tmpObjekt = null;

            foreach (StateObject item in chatObjekte)
            {
                if (item.peerName.Equals(NameDesAusgewaehltenPeers))
                {
                    tmpObjekt = item;
                    gefunden  = true;
                    break;
                }
            }

            if (gefunden)
            {
                //ChatForm chatform = new ChatForm();
                try {
                    ChatForm chatformNeu = new ChatForm();
                    chatformNeu.Text   = "Chat mit " + tmpObjekt.peerName;
                    tmpObjekt.chatForm = chatformNeu;
                    chatformNeu.Show();

                    //Application.Run(chatform);
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }

                //chatform.Text = "Chat mit " + tmpObjekt.peerName;
                //chatform.Show();
                //tmpObjekt.chatForm = chatform;
            }
            else
            {
                ChatForm chatform = new ChatForm();
                chatform.Text = "Chat mit " + NameDesAusgewaehltenPeers;
                chatform.Show();
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(IpOfSelectedPeer), peersPort);
                Socket     client   = new Socket(SocketType.Stream, ProtocolType.Tcp);

                client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
                //connectDone.WaitOne();

                StateObject state = new StateObject();
                state.workSocket = client;
                state.chatForm   = chatform;
                state.peerName   = NameDesAusgewaehltenPeers;
                chatObjekte.Add(state);
                Thread.Sleep(100);
                try {
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallbackChat), state);
                } catch (Exception ex) {
                    Console.WriteLine(ex.ToString());
                }

                Thread.Sleep(100);
                //sendDone.WaitOne();
                sendThreadChat(client);
            }
        }
        private void ReceiveCallback(IAsyncResult ar)
        {
            try {
                sendDone.Set();
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;
                state.sb = new StringBuilder();
                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));
                    //Console.WriteLine("Empfangen: " + state.sb.ToString());
                    string response = state.sb.ToString();

                    int begin = response.IndexOf("beg{");
                    int ende  = response.IndexOf("}end");

                    string responseOhneHeaderUndTailer = "";
                    for (int j = begin + 4; j < ende; j++)
                    {
                        responseOhneHeaderUndTailer += response[j];
                    }
                    int aktion = -1;


                    if (responseOhneHeaderUndTailer.Length > 1)
                    {
                        aktion = Int32.Parse("" + responseOhneHeaderUndTailer[0]);
                    }
                    if (aktion == 2)
                    {
                        string[] ohneAktion        = responseOhneHeaderUndTailer.Split('☻');
                        string[] dateienMitGroesse = ohneAktion[1].Split('♦');

                        Invoke((MethodInvoker) delegate {
                            filesView2.Items.Clear();
                            foreach (string item in dateienMitGroesse)
                            {
                                string[] dateiUndGroesse = item.Split(';');
                                if (dateiUndGroesse.Length == 2)
                                {
                                    ListViewItem newItem = new ListViewItem(Convert.ToString(dateiUndGroesse[0]));
                                    newItem.SubItems.Add(dateiUndGroesse[1]);

                                    filesView2.Items.Add(newItem);
                                }
                            }
                        });
                    }
                    else if (aktion == 3)
                    {
                        string[] mesageItems = responseOhneHeaderUndTailer.Split(':');
                        client.SendFile(mesageItems[3]);
                    }
                    if (Form1.run)
                    {
                        client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                    }
                    else
                    {
                        client.Disconnect(false);
                        client.Close();
                        client.Dispose();
                        Application.Exit();
                    }
                }
                client.Disconnect(false);
                client.Close();
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }