This method is copy-pasted from twisted.names.common.
    The difference with the original is, that it favors IPv4 responses over
    IPv6. This is motivated by the problem of resolving "maps.googleapis.com"
    name, which has both types of entries.
    The logic in twisted.internet.tcp.Connector assumes the IPv4 type of
    address, and it fails to connect if IPv6 address is given.
    This problem only occurs with Twisted 10.2. In 12.1 the Connector
    implementation can already handle both types of addresses.

def extractRecord(resolver, name, answers, level=10):
    '''
    This method is copy-pasted from twisted.names.common.
    The difference with the original is, that it favors IPv4 responses over
    IPv6. This is motivated by the problem of resolving "maps.googleapis.com"
    name, which has both types of entries.
    The logic in twisted.internet.tcp.Connector assumes the IPv4 type of
    address, and it fails to connect if IPv6 address is given.
    This problem only occurs with Twisted 10.2. In 12.1 the Connector
    implementation can already handle both types of addresses.
    '''

    if not level:
        return None
    for r in answers:
        if r.name == name and r.type == dns.A:
            return socket.inet_ntop(socket.AF_INET, r.payload.address)
    for r in answers:
        if r.name == name and r.type == dns.CNAME:
            result = extractRecord(
                resolver, r.payload.name, answers, level - 1)
            if not result:
                return resolver.getHostByName(
                    str(r.payload.name), effort=level - 1)
            return result
    if hasattr(socket, 'inet_ntop'):
        for r in answers:
            if r.name == name and r.type == dns.A6:
                return socket.inet_ntop(socket.AF_INET6, r.payload.address)
        for r in answers:
            if r.name == name and r.type == dns.AAAA:
                return socket.inet_ntop(socket.AF_INET6, r.payload.address)
    # No answers, but maybe there's a hint at who we should be asking about
    # this
    for r in answers:
        if r.type == dns.NS:
            r = Resolver(servers=[(str(r.payload.name), dns.PORT)])
            d = r.lookupAddress(str(name))
            d.addCallback(lambda (ans, auth, add):
                          extractRecord(r, name, ans + auth + add, level - 1))
            return d