import socket


def from_address(address):
    """
    Reverse resolve an address to a hostname.

    Given a string containing an IPv4 or IPv6 address, this functions returns
    a hostname associated with the address, using an LRU cache to speed up
    repeat queries. If the address does not reverse, the function returns
    the original address.

    :param str address: The address to resolve to a hostname
    :returns: The resolved hostname
    """
    if ':' in address:
        # XXX Need to consider what (if anything) we should be doing with the
        # scope-id field here
        sockaddr = (address, 0, 0, 0)
    else:
        sockaddr = (address, 0)
    return socket.getnameinfo(sockaddr, 0)[0]