def parse_addr(addr, port=20000):
    """ Parse IP addresses and ports.
        Works with:
            IPv6 address with and without port;
            IPv4 address with and without port.
    """
    if addr == '':
        # no address given (default: localhost IPv4 or IPv6)
        return "", port, 0
    elif ']:' in addr:
        # IPv6 address with port
        ip, port = addr.rsplit(':', 1)
        return ip.strip('[]'), int(port), 6
    elif ']' in addr:
        # IPv6 address without port
        return addr.strip('[]'), port, 6
    elif addr.count(':') > 1:
        # IPv6 address without port
        return addr, port, 6
    elif ':' in addr:
        # IPv4 address with port
        ip, port = addr.split(':')
        return ip, int(port), 4
    else:
        # IPv4 address without port
        return addr, port, 4