if ($configempty) {
    debugging('No previous configuration detected, attempting auto configuration', DEBUG_DEVELOPER);
    // autodetect sso settings
    if ($base_sso_settings = $drupalauth->detect_sso_settings($config['host_uri'])) {
        //merge in the resulting settings
        $config = $base_sso_settings + $config;
    }
    debugging("using the following settings initially: " . print_r($config, true));
}
// switch these over to objects now that all the merging is done
$defaults = (object) $defaults;
$config = (object) $config;
$endpoint_reachable = false;
$drupalserver = new RemoteAPI($config->host_uri);
// the settings service is public/public and just returns the cookiedomain and user field names (not data)
if ($remote_settings = $drupalserver->Settings()) {
    debugging("Received a cookie value from the remote server: " . print_r($remote_settings, true), DEBUG_DEVELOPER);
    $endpoint_reachable = true;
    //we connected and the service is actively responding
    set_config('host_uri', $config->host_uri, 'auth_drupalservices');
    //if the cookie domain hasn't been previously set, set it now
    if ($config->cookiedomain == '' && $configempty) {
        // the cookiedomain should get received via the Settings call
        $config->cookiedomain = $remote_settings->cookiedomain;
    }
    if ($configempty) {
        set_config('cookiedomain', $config->cookiedomain, 'auth_drupalservices');
    }
} else {
    //TODO: This should get converted into a proper message.
    debugging("The moodlesso service is unreachable. Please verify that you have the Mooodle SSO drupal module installed and enabled: http://drupal.org/project/moodle_sso ", DEBUG_DEVELOPER);