public static CommandBase Parse(byte[] data) { using (var stream = new MemoryStream(data)) using (var reader = new BinaryReader(stream)) { CommandBase command; switch ((CommandType)reader.ReadUInt16()) { case CommandType.JoinCharacter: command = new JoinCharacterCommand(); break; case CommandType.CharacterJoined: command = new CharacterJoinedCommand(); break; case CommandType.CharacterDetected: command = new CharacterDetectedCommand(); break; case CommandType.KeyPressed: command = new KeyPressedCommand(); break; case CommandType.PositionChanged: command = new PositionChangedCommand(); break; default: throw new NotImplementedException(); } command.Token = reader.ReadUInt16(); command.Deserialize(reader); return command; } }