private void OnReceiveCallBack(IAsyncResult ar) { UdpClient u = (UdpClient)ar.AsyncState; IPEndPoint e = new IPEndPoint(IPAddress.Any, 0); try { byte[] receiveBytes = u.EndReceive(ar, ref e); string receiveString = Encoding.ASCII.GetString(receiveBytes); Regex exp = new Regex("BT-SEARCH \\* HTTP/1.1\\r\\nHost: 239.192.152.143:6771\\r\\nPort: (?<port>[^@]+)\\r\\nInfohash: (?<hash>[^@]+)\\r\\n\\r\\n\\r\\n"); Match match = exp.Match(receiveString); if (!match.Success) { return; } int portcheck = Convert.ToInt32(match.Groups["port"].Value); if (portcheck < 0 || portcheck > 65535) { return; } TorrentManager manager = null; InfoHash matchHash = InfoHash.FromHex(match.Groups["hash"].Value); for (int i = 0; manager == null && i < engine.Torrents.Count; i++) { if (engine.Torrents [i].InfoHash == matchHash) { manager = engine.Torrents [i]; } } if (manager == null) { return; } Uri uri = new Uri("tcp://" + e.Address.ToString() + ':' + match.Groups["port"].Value); Peer peer = new Peer("", uri, EncryptionTypes.All); // Add new peer to matched Torrent if (!manager.HasMetadata || !manager.Torrent.IsPrivate) { ClientEngine.MainLoop.Queue(delegate { int count = manager.AddPeersCore(peer); manager.RaisePeersFound(new LocalPeersAdded(manager, count, 1)); }); } } catch { // Failed to receive data, ignore } finally { try { u.BeginReceive(OnReceiveCallBack, ar.AsyncState); } catch { // It's closed } } }