public function unenrol_user($username, $courseid) {
        global $CFG, $DB;

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

        $user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$client->id));

        if ($user === false) {
            throw new mnet_server_exception(5014, 'usernotfound', '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) {
            // if they can not enrol, they can not unenrol
            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);
            $instanceforall = true;
        }

        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');
        }

        if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
            try {
                $enrol->unenrol_user($instance, $user->id);

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

        if (empty($instanceforall)) {
            // if the user was enrolled via 'All hosts' instance and the specific one
            // was created after that, the first enrolment would be kept.
            $instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'mnet', 'customint1'=>0), '*', IGNORE_MISSING);

            if ($instance) {
                // repeat the same procedure for 'All hosts' instance, too. Note that as the host specific
                // instance exists, it will be used for the future enrolments

                if ($DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$user->id))) {
                    try {
                        $enrol->unenrol_user($instance, $user->id);

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

        return true;
    }