def crc(s):
    """Function for computing the CRC used in the protocol.  I haven't looked
    at it closely enough to really see what it's doing..."""
    s += '\x00\x00'
    p = 0
    r1 = ord(s[p])
    p += 1
    r2 = ord(s[p])
    p += 1
    for i in range(len(s) - 2):
        r3 = ord(s[p])
        p += 1
        for j in range(8):
            r4 = r1
            r1 = r1 << 1 & 255
            if r2 & 128 != 0:
                r1 = r1 + 1 & 255
            r2 = r2 << 1 & 255
            if r3 & 128 != 0:
                r2 = r2 + 1 & 255
            r3 = r3 << 1 & 255
            if r4 & 128 != 0:
                r1 = r1 ^ 16
                r2 = r2 ^ 65
    return r1 << 8 | r2
