/// <summary>
        /// Callback to receive the network-session search results from quick-match.
        /// </summary>
        void QuickMatchSearchCompleted(object sender, OperationCompletedEventArgs e)
        {
            try
            {
                AvailableNetworkSessionCollection availableSessions =
                    NetworkSession.EndFind(e.AsyncResult);
                if ((availableSessions != null) && (availableSessions.Count > 0))
                {
                    // join the session
                    try
                    {
                        IAsyncResult asyncResult = NetworkSession.BeginJoin(
                            availableSessions[0], null, null);

                        // create the busy screen
                        NetworkBusyScreen busyScreen = new NetworkBusyScreen(
                            "Joining the session...", asyncResult);
                        busyScreen.OperationCompleted += QuickMatchSessionJoined;
                        ScreenManager.AddScreen(busyScreen);
                    }
                    catch (NetworkException ne)
                    {
                        const string     message    = "Failed joining the session.";
                        MessageBoxScreen messageBox = new MessageBoxScreen(message);
                        messageBox.Accepted  += FailedMessageBox;
                        messageBox.Cancelled += FailedMessageBox;
                        ScreenManager.AddScreen(messageBox);

                        System.Console.WriteLine("Failed to join session:  " +
                                                 ne.Message);
                    }
                    catch (GamerPrivilegeException gpe)
                    {
                        const string message =
                            "You do not have permission to join a session.";
                        MessageBoxScreen messageBox = new MessageBoxScreen(message);
                        messageBox.Accepted  += FailedMessageBox;
                        messageBox.Cancelled += FailedMessageBox;
                        ScreenManager.AddScreen(messageBox);

                        System.Console.WriteLine(
                            "Insufficient privilege to join session:  " + gpe.Message);
                    }
                }
                else
                {
                    const string     message    = "No matches were found.";
                    MessageBoxScreen messageBox = new MessageBoxScreen(message);
                    messageBox.Accepted  += FailedMessageBox;
                    messageBox.Cancelled += FailedMessageBox;
                    ScreenManager.AddScreen(messageBox);
                }
            }
            catch (GamerPrivilegeException gpe) {
                MessageBoxScreen messageBox = new MessageBoxScreen(gpe.Message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);
            }
        }
        /// <summary>
        /// Callback to receive the network-session search results.
        /// </summary>
        internal void SessionsFound(object sender, OperationCompletedEventArgs e)
        {
            try
            {
                availableSessions = NetworkSession.EndFind(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string     message    = "Failed searching for the session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to search for session:  " +
                                         ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to search for a session. ";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + gpe.Message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to search for session:  " + gpe.Message);
            }
            MenuEntries.Clear();
            if (availableSessions != null)
            {
                foreach (AvailableNetworkSession availableSession in
                         availableSessions)
                {
                    if (availableSession.CurrentGamerCount < World.MaximumPlayers)
                    {
                        MenuEntries.Add(availableSession.HostGamertag + " (" +
                                        availableSession.CurrentGamerCount.ToString() + "/" +
                                        World.MaximumPlayers.ToString() + ")");
                    }
                    if (MenuEntries.Count >= maximumSessions)
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Finishes the asynchronous process of joining a game from an invitation,
        /// joining the lobby of a hosted game if the join was successful.
        /// </summary>
        void InvitedSessionJoined(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;

            try
            {
                networkSession = NetworkSession.EndJoinInvited(e.AsyncResult);
            }
            catch (NetworkSessionJoinException je)
            {
                const string     message    = "Failed joining the session (";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + je.JoinError.ToString() + ").");
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to join session:  " +
                                         je.Message);
            }
            catch (Exception ge)
            {
                const string     message    = "Failed joining the session (";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + ge.Message + ").");
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to join session:  " +
                                         ge.Message);
            }

            // Start the lobby if we got the session!
            // Otherwise the MainMenuScreen will be available.
            if (networkSession != null)
            {
                LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
                lobbyScreen.ScreenManager = ScreenManager;
                ScreenManager.AddScreen(lobbyScreen);
            }
        }
        /// <summary>
        /// Callback when a session is created.
        /// </summary>
        void SessionCreated(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;

            try
            {
                networkSession = NetworkSession.EndCreate(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string     message    = "Failed creating the session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to create session:  " +
                                         ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to create a session. ";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + gpe.Message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to create session:  " + gpe.Message);
            }
            if (networkSession != null)
            {
                networkSession.AllowHostMigration  = true;
                networkSession.AllowJoinInProgress = false;
                LoadLobbyScreen(networkSession);
            }
        }
        /// <summary>
        /// Callback to load the lobby screen with the new session.
        /// </summary>
        private void LoadLobbyScreen(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;

            try
            {
                networkSession = NetworkSession.EndJoin(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string     message    = "Failed joining session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed joining session:  " + ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to join a session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted  += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to join session:  " + gpe.Message);
            }
            if (networkSession != null)
            {
                LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
                lobbyScreen.ScreenManager = this.ScreenManager;
                ScreenManager.AddScreen(lobbyScreen);
            }
        }
        /// <summary>
        /// Callback to load the lobby screen with the new session.
        /// </summary>
        private void LoadLobbyScreen(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;
            try
            {
                networkSession = NetworkSession.EndJoin(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string message = "Failed joining session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed joining session:  " + ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to join a session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to join session:  " + gpe.Message);
            }
            if (networkSession != null)
            {
                LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
                lobbyScreen.ScreenManager = this.ScreenManager;
                ScreenManager.AddScreen(lobbyScreen);
            }
        }
        /// <summary>
        /// Callback to receive the network-session search results.
        /// </summary>
        internal void SessionsFound(object sender, OperationCompletedEventArgs e)
        {
            try
            {
                availableSessions = NetworkSession.EndFind(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string message = "Failed searching for the session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to search for session:  " +
                    ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to search for a session. ";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + gpe.Message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to search for session:  " + gpe.Message);
            } 
            MenuEntries.Clear();
            if (availableSessions != null)
            {
                foreach (AvailableNetworkSession availableSession in
                    availableSessions)
                {
                    if (availableSession.CurrentGamerCount < World.MaximumPlayers)
                    {
                        MenuEntries.Add(availableSession.HostGamertag + " (" +
                            availableSession.CurrentGamerCount.ToString() + "/" +
                            World.MaximumPlayers.ToString() + ")");
                    }
                    if (MenuEntries.Count >= maximumSessions)
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Finishes the asynchronous process of joining a game from an invitation,
        /// joining the lobby of a hosted game if the join was successful.
        /// </summary>
        void InvitedSessionJoined(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;
            try
            {
                networkSession = NetworkSession.EndJoinInvited(e.AsyncResult);
            }
            catch (NetworkSessionJoinException je)
            {
                const string message = "Failed joining the session (";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + je.JoinError.ToString() + ").");
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to join session:  " +
                    je.Message);
            }
            catch (Exception ge)
            {
                const string message = "Failed joining the session (";
                MessageBoxScreen messageBox = new MessageBoxScreen(message + ge.Message + ").");
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to join session:  " +
                    ge.Message);
            }

            // Start the lobby if we got the session!
            // Otherwise the MainMenuScreen will be available.
            if (networkSession != null)
            {
                LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
                lobbyScreen.ScreenManager = ScreenManager;
                ScreenManager.AddScreen(lobbyScreen);
            }
        }
        /// <summary>
        /// Callback when a session is created.
        /// </summary>
        void SessionCreated(object sender, OperationCompletedEventArgs e)
        {
            NetworkSession networkSession = null;
            try
            {
                networkSession = NetworkSession.EndCreate(e.AsyncResult);
            }
            catch (NetworkException ne)
            {
                const string message = "Failed creating the session.";
                MessageBoxScreen messageBox = new MessageBoxScreen(message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine("Failed to create session:  " +
                    ne.Message);
            }
            catch (GamerPrivilegeException gpe)
            {
                const string message =
                    "You do not have permission to create a session. ";
                MessageBoxScreen messageBox = new MessageBoxScreen(message+gpe.Message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);

                System.Console.WriteLine(
                    "Insufficient privilege to create session:  " + gpe.Message);
            }
            if (networkSession != null)
            {
                networkSession.AllowHostMigration = true;
                networkSession.AllowJoinInProgress = false;
                LoadLobbyScreen(networkSession);
            }
        }
        /// <summary>
        /// Callback to receive the network-session search results from quick-match.
        /// </summary>
        void QuickMatchSearchCompleted(object sender, OperationCompletedEventArgs e)
        {
            try
            {
                AvailableNetworkSessionCollection availableSessions =
                    NetworkSession.EndFind(e.AsyncResult);
                if ((availableSessions != null) && (availableSessions.Count > 0))
                {
                    // join the session
                    try
                    {
                        IAsyncResult asyncResult = NetworkSession.BeginJoin(
                            availableSessions[0], null, null);

                        // create the busy screen
                        NetworkBusyScreen busyScreen = new NetworkBusyScreen(
                            "Joining the session...", asyncResult);
                        busyScreen.OperationCompleted += QuickMatchSessionJoined;
                        ScreenManager.AddScreen(busyScreen);
                    }
                    catch (NetworkException ne)
                    {
                        const string message = "Failed joining the session.";
                        MessageBoxScreen messageBox = new MessageBoxScreen(message);
                        messageBox.Accepted += FailedMessageBox;
                        messageBox.Cancelled += FailedMessageBox;
                        ScreenManager.AddScreen(messageBox);

                        System.Console.WriteLine("Failed to join session:  " +
                            ne.Message);
                    }
                    catch (GamerPrivilegeException gpe)
                    {
                        const string message =
                            "You do not have permission to join a session.";
                        MessageBoxScreen messageBox = new MessageBoxScreen(message);
                        messageBox.Accepted += FailedMessageBox;
                        messageBox.Cancelled += FailedMessageBox;
                        ScreenManager.AddScreen(messageBox);

                        System.Console.WriteLine(
                            "Insufficient privilege to join session:  " + gpe.Message);
                    }
                }
                else
                {
                    const string message = "No matches were found.";
                    MessageBoxScreen messageBox = new MessageBoxScreen(message);
                    messageBox.Accepted += FailedMessageBox;
                    messageBox.Cancelled += FailedMessageBox;
                    ScreenManager.AddScreen(messageBox);
                }
            }
            catch (GamerPrivilegeException gpe){
                MessageBoxScreen messageBox = new MessageBoxScreen(gpe.Message);
                messageBox.Accepted += FailedMessageBox;
                messageBox.Cancelled += FailedMessageBox;
                ScreenManager.AddScreen(messageBox);
            }
        }