private static void ConnectToPeers(List <PeerInfo> peers, Torrent torrent) { foreach (PeerInfo peerInfo in peers) { Peer remotePeer = new Peer(peerInfo, torrent, PeerId.GetDefaultPeerId()); Console.WriteLine("Connecting to {0}:{1}", peerInfo.IpAddress, peerInfo.Port); remotePeer.Connected += delegate(object sender, EventArgs args) { Peer peer = sender as Peer; Console.WriteLine("Connected to peer at {0} on port {1}", peer.Info.IpAddress, (ushort)peer.Info.Port); }; remotePeer.HandshakeReceived += delegate(object sender, PeerEventArgs args) { Peer peer = sender as Peer; Console.WriteLine("Received handshake from {0}", peer.Info.IpAddress); }; remotePeer.Error += delegate(object sender, PeerEventArgs args) { Peer peer = sender as Peer; System.Diagnostics.Debug.WriteLine(string.Format("Error from {0}: {1}", peer.Info.IpAddress, args.Exception.Message)); }; remotePeer.Connect(); } }