/* returns null if:
         *      - already logged in
         *      - database connection failed
         *      - database authentication failed
         *      - user data is null
         *      - username is null
         */
        public string Login(string address, string username, string password, out ECode ec)
        {
            ec = ECode.None;
            string token = null;
            IUserPortalServerController_Callback callbackChannel
                = OperationContext.Current.GetCallbackChannel <IUserPortalServerController_Callback>();

            try
            {
                // Check credentials against database //
                int id = DSConnection.AuthenticateCredentials(username, password);
                if (id != -1) // valid
                {
                    // Check if already logged in //
                    if (UserRegistry.IsPuidRegistered(username) == false)
                    {
                        // Log in User //
                        User        user    = DSConnection.GetUser(id);
                        UserSession session = new UserSession(username, address, USER_LIFESPAN, callbackChannel, user);
                        token = UserRegistry.RegisterClient(session);
                        Console.WriteLine("> New Login: @username:{" + username + "} token:{" + token + "}");
                    }
                    else
                    {                                                                                          // Retrieve existing session token //
                        token = UserRegistry.GetToken(username);
                        ((UserSession)UserRegistry.GetClientSession(token)).CallbackChannel = callbackChannel; // update callback channel
                        Console.WriteLine("> Old Login: @username:{" + username + "} token:{" + token + "}");
                    }
                }
                else
                {
                    Console.WriteLine("> Failed Login: @username:{" + username + "}");
                }
            } catch (DataServerException ex) {
                // occurs if any of the database queries fail
                ec = ex.ErrorCode;
            } catch (ArgumentNullException) {
                // occurs if username or user data is null
                ec = ECode.QueryError;
            }
            return(token);
        }
        public string RegisterGameServer(string address, string serverName, out ECode ec)
        {
            ec = ECode.None;
            string token     = null;
            bool   collision = false;

            try
            {
                // Check for GameServer collisions //
                collision = (GameRegistry.IsPuidRegistered(serverName)); // Check if already registered
                if (collision == false)
                {
                    // Check for server address collisions
                    foreach (KeyValuePair <string, ClientSession> kvp in GameRegistry)
                    {
                        if (collision == false) // If no collision detected, check for collision
                        {
                            collision = (kvp.Value.ClientAddress == address);
                        }
                    }
                }

                if (collision)
                {
                    ec = ECode.QueryError;
                    Console.WriteLine("> GameServer registration collision: @ServerName:{" + serverName + "}");
                }
                else
                {
                    IGamePortalServerController_Callback callbackChannel
                        = OperationContext.Current.GetCallbackChannel <IGamePortalServerController_Callback>();
                    GameSession session = new GameSession(serverName, address, GAME_LIFESPAN, callbackChannel);
                    token = GameRegistry.RegisterClient(session);
                    Console.WriteLine("> New GameServer registration: @ServerName:{" + serverName + "} token:{" + token + "}");
                }
            } catch (ArgumentNullException) {
                // occurs if serverName or address is null
                ec = ECode.QueryError;
            }

            return(token);
        }