import struct
import io


def _recv(conn):
    """ Reads bytes data from socket connection

    Expects to receive the size of the data before the data itself.
    Data length is 4 bytes unsigned long integer.

    Parameters
    ----------
    conn : socket.socket

    Returns
    -------
    bytes
        Data received read from the socket.
    """

    conn = conn.makefile('rb')

    # Read incoming data size
    struct_size = struct.calcsize('<L')
    data_len = struct.unpack('<L', conn.read(struct_size))[0]

    # Write data from connection to stream
    stream = io.BytesIO()
    stream.write(conn.read(data_len))

    stream.seek(0)
    return stream.read()