<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_to_stream(stream, response, block_len=1024):
 """ Send the response in blocks of the given len using the passed method dependending on the socket type. """

        try:
            i_start = 0
            i_end = block_len
            while True:
                if i_end > len(response):
                    stream(response[i_start:])
                    break
                stream(response[i_start:i_end])
                i_start = i_end
                i_end += block_len
        except (socket.timeout, socket.error) as exception:
            logger.debug('Error sending response to the client: %s', exception)