private async Task GetAndParseReannounceResponseAsync(string message, int numWant)
        {
            var trackerResponse = new TrackerResponse();
            await trackerResponse.GetTrackerResponse(this, ownerForm.myPeerID, 25000,
                                                     message, numWant).ConfigureAwait(false);

            var parser = new BencodeParser();

            foreach (var item in trackerResponse.response)
            {
                switch (item.Key.ToString())
                {
                case "interval":
                    trackerInterval = parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    if (trackerInterval >= 600)
                    {
                        trackerTimer.Change(trackerInterval * 1000, trackerInterval * 1000);
                    }
                    else
                    {
                        // if for some reason we didn't get the interval,
                        // or if it's too small, set it to 40 minutes
                        trackerTimer.Change(40 * 60 * 1000, 40 * 60 * 1000);
                    }
                    break;

                case "min interval":
                    trackerMinInterval = parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    break;

                case "tracker id":
                    trackerID = parser.Parse <BString>(item.Value.EncodeAsBytes()).ToString();
                    break;
                }
            }
        }
        private async Task GetAndParseTrackerResponseAsync(bool isReannounce)
        {
            var    trackerResponse = new TrackerResponse();
            string trackerEvent    = null;

            if (!isReannounce)
            {
                trackerEvent = "started";
            }
            await trackerResponse.GetTrackerResponse(this, ownerForm.myPeerID,
                                                     25000, trackerEvent, 50).ConfigureAwait(false);

            var parser = new BencodeParser();
            int complete = 0, incomplete = 0;

            foreach (var item in trackerResponse.response)
            {
                switch (item.Key.ToString())
                {
                case "failure reason":
                    // something went wrong
                    if (!isReannounce)
                    {
                        ownerForm.ShowError(MainForm.TRACKERERRORMSG + item.Value.ToString());
                    }
                    return;

                case "interval":
                    trackerInterval = parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    break;

                case "min interval":
                    trackerMinInterval = parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    break;

                case "tracker id":
                    trackerID = parser.Parse <BString>(item.Value.EncodeAsBytes()).ToString();
                    break;

                case "complete":
                    complete = (int)parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    // number of seeders (peers with completed file). Only for UI purposes I guess...
                    break;

                case "incomplete":
                    incomplete = (int)parser.Parse <BNumber>(item.Value.EncodeAsBytes()).Value;
                    // number of leechers; purpose is the same
                    break;

                case "peers":
                    peersAddr.Clear();
                    var peers = parser.Parse(item.Value.EncodeAsBytes());
                    if (peers is BString)
                    {
                        byte[] binaryPeersList;
                        binaryPeersList = ((BString)peers).Value.ToArray();
                        if (binaryPeersList.Length % 6 != 0)
                        {
                            // not actually an invalid bencoding, but for simplicity
                            throw new InvalidBencodeException <BObject>();
                        }
                        else
                        {
                            byte[] oneEntry = new byte[6];
                            for (int i = 0; i < binaryPeersList.Length; i += 6)
                            {
                                Array.Copy(binaryPeersList, i, oneEntry, 0, 6);
                                peersAddr.AddLast(GetPeerFromBytes(oneEntry));
                            }
                        }
                    }
                    else if (peers is BList)
                    {
                        foreach (var peerEntry in (BList)peers)
                        {
                            if (peerEntry is BDictionary)
                            {
                                string IP   = parser.Parse <BString>(((BDictionary)peerEntry)["ip"].EncodeAsBytes()).ToString();
                                long   port = parser.Parse <BNumber>(((BDictionary)peerEntry)["port"].EncodeAsBytes()).Value;
                                peersAddr.AddLast(new IPEndPoint(IPAddress.Parse(IP), (int)port));
                            }
                        }
                    }
                    else
                    {
                        // not actually an invalid bencoding, but for simplicity
                        throw new InvalidBencodeException <BObject>();
                    }
                    break;
                }
            }
            ownerForm.UpdateSeedersLeechersNum(this, complete, incomplete);
        }