public static function generateRetrieveMultipleRequest( $queryXML, $pagingCookie = null, $limitCount = null, $pageNumber = null ) {
        /* Turn the queryXML into a DOMDocument so we can manipulate it */
        $queryDOM = new DOMDocument();
        $queryDOM->loadXML( $queryXML );

        $newPage = 1;
        if ( $queryDOM->documentElement->hasAttribute( 'page' ) ) {
            $newPage = (int)$queryDOM->documentElement->getAttribute( 'page' );
        }

        if ( $pagingCookie !== null ) {
            $newPage = Client::getPageNo( $pagingCookie ) + 1;
            $queryDOM->documentElement->setAttribute( 'paging-cookie', $pagingCookie );
        } elseif ( $pageNumber !== null ) {
            $newPage = $pageNumber;
        }

        /* Modify the query that we send: Add the Page number */
        $queryDOM->documentElement->setAttribute( 'page', $newPage );

        /* Find the current limit, if there is one */
        $currentLimit = Client::getMaximumRecords() + 1;
        if ( $queryDOM->documentElement->hasAttribute( 'count' ) ) {
            $currentLimit = $queryDOM->documentElement->getAttribute( 'count' );
        }

        /* Determine the preferred limit (passed by argument, or 5000 if not set) */
        $preferredLimit = ( $limitCount == null ) ? $currentLimit : $limitCount;
        if ( $preferredLimit > Client::getMaximumRecords() ) {
            $preferredLimit = Client::getMaximumRecords();
        }

        /* If the current limit is not set, or is greater than the preferred limit, override it */
        if ( $currentLimit > $preferredLimit ) {
            /* Modify the query that we send: Change the Count */
            $queryDOM->documentElement->setAttribute( 'count', $preferredLimit );
            /* Update the Query XML with the new structure */
        }

        $queryXML = $queryDOM->saveXML( $queryDOM->documentElement );

        /* Generate the RetrieveMultipleRequest message */
        $retrieveMultipleRequestDOM = new DOMDocument();
        $retrieveMultipleNode       = $retrieveMultipleRequestDOM->appendChild( $retrieveMultipleRequestDOM->createElementNS( 'http://schemas.microsoft.com/xrm/2011/Contracts/Services', 'RetrieveMultiple' ) );
        $queryNode                  = $retrieveMultipleNode->appendChild( $retrieveMultipleRequestDOM->createElement( 'query' ) );
        $queryNode->setAttributeNS( 'http://www.w3.org/2001/XMLSchema-instance', 'i:type', 'b:FetchExpression' );
        $queryNode->setAttributeNS( 'http://www.w3.org/2000/xmlns/', 'xmlns:b', 'http://schemas.microsoft.com/xrm/2011/Contracts' );
        $queryNode->appendChild( $retrieveMultipleRequestDOM->createElement( 'b:Query', trim( htmlentities( $queryXML ) ) ) );

        /* Return the DOMNode */

        return $retrieveMultipleNode;
    }