def receive_data_in_chunks(sock, buffersize):
    """Receive data in chunks of size buffersize from the socket"""
    chunk = sock.recv(buffersize)
    chunks = [chunk]
    while len(chunk.strip()):
        chunk = sock.recv(buffersize)
        chunks.append(chunk)
    data = b''.join(chunks).strip()
    return data
