def _compat_inet_pton(family, addr):
    """ socket.inet_pton for platforms that don't have it """

    if family == socket.AF_INET:
        # inet_aton accepts some strange forms, so we use our own
        res = _compat_bytes('')
        parts = addr.split('.')
        if len(parts) != 4:
            raise ValueError('Expected 4 dot-separated numbers')

        for part in parts:
            intval = int(part, 10)
            if intval < 0 or intval > 0xff:
                raise ValueError("Invalid integer value in IPv4 address: " + str(intval))

            res = res + struct.pack('!B', intval)

        return res
    elif family == socket.AF_INET6:
        wordcount = 8
        res = _compat_bytes('')

        # IPv4 embedded?
        dotpos = addr.find('.')
        if dotpos >= 0:
            v4start = addr.rfind(':', 0, dotpos)
            if v4start == -1:
                raise ValueException("Missing colons in an IPv6 address")
            wordcount = 6
            res = socket.inet_aton(addr[v4start+1:])
            addr = addr[:v4start] + '!' # We leave a marker that the address is not finished

        # Compact version?
        compact_pos = addr.find('::')
        if compact_pos >= 0:
            if compact_pos == 0:
                addr = '0' + addr
                compact_pos += 1
            if compact_pos == len(addr)-len('::'):
                addr = addr + '0'

            addr = (addr[:compact_pos] + ':' +
                ('0:' * (wordcount - (addr.count(':') - '::'.count(':')) - 2))
                + addr[compact_pos + len('::'):])

        # Remove any dots we left
        if addr.endswith('!'):
            addr = addr[:-len('!')]

        words = addr.split(':')
        if len(words) != wordcount:
            raise ValueError('Invalid number of IPv6 hextets, expected ' + str(wordcount) + ', got ' + str(len(words)))
        for w in reversed(words):
            # 0x and negative is not valid here, but accepted by int(,16)
            if 'x' in w or '-' in w:
                raise ValueError("Invalid character in IPv6 address")

            intval = int(w, 16)
            if intval > 0xffff:
                raise ValueError("IPv6 address componenent too big")
            res = struct.pack('!H', intval) + res

        return res

    else:
        raise ValueError("Unknown protocol family " + family)
socket.inet_pton for platforms that don't have it