void OnReceived(UdpReceiveResult res) { if (!finished) { ReceiveNext(); } lock (this) { if (Helper.MakeChoice(Helper.PLP)) // Simulate packet loss { Message?.Invoke("Simulated packet loss. ", MType.Important); return; } int seq; bool valid = Helper.Extract(res.Buffer, ref buffer, out seq); Message?.Invoke("Received PKT: " + seq + (valid ? "" : "? (Corrupt)"), valid ? MType.ExtraDetail : MType.Important); if (valid) { if (seq == 0) { if (metareceived) { SendACK(0); } else { // Establish connection using new socket fudp.Connect(res.RemoteEndPoint); rttask.SetAction(() => SendACK(0)); OnMetaReceived(); } } else { if (rttask.Running) { rttask.Stop(); } OnDataReceived(valid, seq); } } else { if (metareceived) { OnDataReceived(valid, seq); } } } }