def shutdown_host(port, username=None, password=None, authdb=None):
    """
    Send the shutdown command to a mongod or mongos on given port.

    This function can be called as a separate thread.
    """
    host = 'localhost:%i' % port
    try:
        if username and password and authdb:
            if authdb != "admin":
                raise RuntimeError("given username/password is not for "
                                    "admin database")
            mc = MongoConnection(host, username=username, password=password)
        else:
            mc = MongoConnection(host)