def _safe_recv(sock, count):
    """
    Wraps sock.recv() handling short reads.

    :param sock: a socket to recv() from
    :param count: the number of bytes to ask for
    :return: a byte string with those bytes
    """
    result = sock.recv(count)
    bytes_read = len(result)
    if 0 < bytes_read < count:
        return result + _safe_recv(sock, count - bytes_read)
    return result