private void finishTransfer()
            {
                FilesAndStatus fas = null;

                foreach (FilesAndStatus tempFas in downloadFiles)
                {
                    if (tempFas.FileName.Equals(fileName))
                    {
                        fas = tempFas;
                    }
                }

                if (!vaildFlag)
                {
                    fas.PbValue = 0;
                    fas.Status  = "Error";
                    MessageBox.Show("There was a problem with " + fileName + "transfer");
                }

                else
                {
                    stopWatch.Stop();
                    TimeSpan ts = stopWatch.Elapsed;
                    // Format and display the TimeSpan value.
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}",
                                                       ts.Hours, ts.Minutes, ts.Seconds);
                    fas.BitRate           = fas.FileSize / ts.Milliseconds;
                    fas.Status            = "Completed";
                    fas.TotalTransferTime = elapsedTime;
                    fileStream.Close();

                    if (fileName.Trim().Equals("reflection.dll"))
                    {
                        while (bwReflactionButton.IsBusy)
                        {
                            ;
                        }
                        bwReflactionButton.RunWorkerAsync();
                    }
                }
                while (bw.IsBusy)
                {
                    ;
                }
                bw.RunWorkerAsync();
            }
            private async void connectToPear(Pear p, long fromByte, long toByte, int num)
            {
                long           startPos = fromByte;
                FilesAndStatus fas      = null;

                foreach (FilesAndStatus tempFas in downloadFiles)
                {
                    if (tempFas.FileName.Equals(fileName))
                    {
                        fas = tempFas;
                    }
                }

                TcpClient client = new TcpClient();
                await client.ConnectAsync(p.Ip, p.Port);

                NetworkStream ns = client.GetStream();

                sendFileNameAndPart(ns, fileName, fromByte, toByte);

                int i = 1;

                try
                {
                    //loop till the Full bytes have been read
                    while (startPos < toByte && fileStream.CanWrite && activeFlag)
                    {
                        byte[] buffer = new byte[50000];
                        i = ns.Read(buffer, 0, buffer.Length);
                        lock (_lock)
                        {
                            downloaded += i;
                            fileStream.Seek(startPos, 0);
                            fileStream.Write(buffer, 0, (int)i);
                        }

                        lock (_lock)
                        {
                            double pctread = ((double)downloaded / tfd.FileSize) * 100;
                            fas.PbValue = Convert.ToInt32(pctread);
                            while (bw.IsBusy)
                            {
                                ;
                            }
                            bw.RunWorkerAsync();
                        }
                        startPos += i;
                    }
                }
                catch (Exception e)
                {
                    vaildFlag = false;
                    fileStream.Close();
                    Console.WriteLine(e);
                    //MessageBoxResult result = MessageBox.Show("There was a problem with " + fileName + "transfer", "Alert");
                }
                finally
                {
                    ns.Close();
                    autoEvents[num - 1].Set();
                }
            }
            private async void startSendFile(ClientUploadDownload cup, NetworkStream ns)
            {
                FileStream     fileStream = null;
                FilesAndStatus fas        = null;

                foreach (FilesAndStatus tempFas in uploadFiles)
                {
                    if (tempFas.FileName.Equals(cup.FileName))
                    {
                        fas = tempFas;
                    }
                }

                try
                {
                    string   path = currentUser.UpLoc + "\\" + cup.FileName;
                    FileInfo file = new FileInfo(path);
                    fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);

                    long   total     = cup.ToByte - cup.FromByte;
                    long   ToatlSent = 0;
                    long   totalLeft = total;
                    int    len       = 0;
                    byte[] buffer    = new byte[50000];

                    fileStream.Seek(cup.FromByte, 0);
                    while (ToatlSent < total && ns.CanWrite && activeFlag)
                    {
                        //Read from the File (len contains the number of bytes read)
                        if (totalLeft > 50000)
                        {
                            len = fileStream.Read(buffer, 0, buffer.Length);
                        }
                        else
                        {
                            len = fileStream.Read(buffer, 0, (int)totalLeft);
                        }
                        //Write the Bytes on the Socket
                        await ns.WriteAsync(buffer, 0, len);

                        //Increase the bytes Read counter
                        ToatlSent += len;

                        double pctread = ((double)ToatlSent / total) * 100;
                        fas.PbValue = Convert.ToInt32(pctread);
                        fas.Status  = "Uploading";

                        while (bw.IsBusy)
                        {
                            ;
                        }
                        bw.RunWorkerAsync();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("A Exception occured in transfer" + e.ToString());
                    fas.Status = "Error";
                    while (bw.IsBusy)
                    {
                        ;
                    }
                    bw.RunWorkerAsync();
                    MessageBoxResult result = MessageBox.Show("There was a problem with " + cup.FileName + "transfer");
                }
                finally
                {
                    fas.Status = "Completed";
                    while (bw.IsBusy)
                    {
                        ;
                    }
                    bw.RunWorkerAsync();

                    ns.Close();
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                        fileStream.Close();
                    }
                }
            }