private void OnMessageReceived(Packet recv) { // need to be on UI thread if (this.InvokeRequired) { this.Invoke(new Action(delegate { OnMessageReceived(recv); })); return; } switch (recv.GetCommand()) { case PacketCommand.NOTIFY_CONNECT_SUCCESS: // Connection successful, wait for notification to begin cmdConnect.Text = "Waiting for opponent..."; break; case PacketCommand.NOTIFY_CONNECT_FAILED: // Connection failed, reset form ConnectResult reason; using (var reader = recv.GetStream()) { reason = (ConnectResult)reader.ReadByte(); } fraConnect.Enabled = true; fraHost.Enabled = true; cmdConnect.Text = "Connect"; MessageBox.Show("Error: " + Peer.ConnectResultToString(reason), ":(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; case PacketCommand.NOTIFY_CONNECTION_LOST: case PacketCommand.NOTIFY_DISCONNECTED: fraConnect.Enabled = true; fraHost.Enabled = true; MessageBox.Show("Lost connection.", ":(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; case (PacketCommand)GameCommand.BeginSession: new FormGame().Show(this); this.Hide(); break; } }