// Request a PQ pair number
function requestPQ(callback, connection) {
    // Create a nonce for the client
    var clientNonce = utility.createNonce(16);
    mtproto.service.req_pq({
        props: {
            nonce: clientNonce
        },
        conn: connection,
        callback: function (ex, resPQ) {
            if (clientNonce === resPQ.nonce) {
                var context = {
                    resPQ: resPQ,
                    connection: connection
                };
                callback(null, context);
            } else {
                callback(createError('Nonce mismatch.', 'ENONCE'));
            }
        }
    });
}
// Request server DH parameters
function requestDHParams(callback, context) {
    var resPQ = context.resPQ;
    mtproto.service.req_DH_params({
        props: {
            nonce: resPQ.nonce,
            server_nonce: resPQ.server_nonce,
            p: context.pBuffer,
            q: context.qBuffer,
            public_key_fingerprint: context.fingerprint,
            encrypted_data: context.encryptedData
        },
        conn: context.connection,
        callback: function (ex, serverDHParams, duration) {
            if (ex) {
                logger.error(ex);
                if (callback) {
                    callback(ex);
                }
            } else {
                if (serverDHParams.typeName === 'mtproto.type.Server_DH_params_ok') {
                    if (logger.isDebugEnabled()) {
                        logger.debug('\'Server_DH_params_ok\' received from Telegram.');
                    }
                    context.serverDHParams = serverDHParams;
                    context.reqDHDuration = duration;
                    callback(null, context);
                } else if (serverDHParams.typeName === 'mtproto.type.Server_DH_params_ko') {
                    logger.warn('\'Server_DH_params_ko\' received from Telegram!');
                    callback(createError(JSON.stringify(serverDHParams), 'EDHPARAMKO'));
                } else {
                    var msg = 'Unknown error received from Telegram!';
                    logger.error(msg);
                    callback(createError(msg, 'EUNKNOWN'));
                }
            }
        }
    });
}