public function enrol_user(array $userdata, $courseid) {
        global $CFG, $DB;
        require_once(__DIR__.'/lib.php');

        if (!$client = get_mnet_remote_client()) {
            die('Callable via XML-RPC only');
        }

        if (empty($userdata['username'])) {
            throw new mnet_server_exception(5021, 'emptyusername', 'enrol_mnet');
        }

        // do we know the remote user?
        $user = $DB->get_record('user', array('username'=>$userdata['username'], 'mnethostid'=>$client->id));

        if ($user === false) {
            // here we could check the setting if the enrol_mnet is allowed to auto-register
            // users {@link http://tracker.moodle.org/browse/MDL-21327}
            $user = mnet_strip_user((object)$userdata, mnet_fields_to_import($client));
            $user->mnethostid = $client->id;
            $user->auth = 'mnet';
            $user->confirmed = 1;
            try {
                $user->id = $DB->insert_record('user', $user);
            } catch (Exception $e) {
                throw new mnet_server_exception(5011, 'couldnotcreateuser', 'enrol_mnet');
            }
        }

        if (! $course = $DB->get_record('course', array('id'=>$courseid))) {
            throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet');
        }

        $courses = $this->available_courses();
        $isavailable = false;
        foreach ($courses as $available) {
            if ($available->remoteid == $course->id) {
                $isavailable = true;
                break;
            }
        }
        if (!$isavailable) {
            throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet');
        }

        // try to load host specific enrol_mnet instance first
        $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>$client->id), '*', IGNORE_MISSING);

        if ($instance === false) {
            // if not found, try to load instance for all hosts
            $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);
        }

        if ($instance === false) {
            // this should not happen as the course was returned by {@see self::available_courses()}
            throw new mnet_server_exception(5017, 'noenrolinstance', 'enrol_mnet');
        }

        if (!$enrol = enrol_get_plugin('mnet')) {
            throw new mnet_server_exception(5018, 'couldnotinstantiate', 'enrol_mnet');
        }

        try {
            $enrol->enrol_user($instance, $user->id, $instance->roleid, time());

        } catch (Exception $e) {
            throw new mnet_server_exception(5019, 'couldnotenrol', 'enrol_mnet', $e->getMessage());
        }

        return true;
    }