void
read_response(Connection *connection, Statistics *stats, char *buffer, int buffer_len)
{
    int bytes_read = 0, bad_count = 0, msg_count = 0, close_count = 0;

    bytes_read = read(connection->sd, buffer, buffer_len - 1);

    if (bytes_read < 0) {
        error("Error reading from socket for connection %d\n", connection->index);
        stats->connections--;
        reopen_connection(connection);
        return;
    }

    if (bytes_read == 0) { // server disconnected us
        // reconnect
        info("Server disconnected as requested %d.\n", connection->index);
        stats->connections--;
        reopen_connection(connection);
        return;
    }

    stats->bytes_read += bytes_read;
    buffer[bytes_read] = '\0';
    debug("Read %d bytes\n", bytes_read);
    trace("Read Message: %s\n", buffer);

    bad_count = count_strinstr(buffer, "HTTP/1.1 4");
    bad_count += count_strinstr(buffer, "HTTP/1.1 5");

    if (bad_count > 0) {
        info("Recevied error. Buffer is %s\n", buffer);
        stats->connections--;
        reopen_connection(connection);
        return;
    }

    msg_count = count_strinstr(buffer, "**MSG**");
    stats->messages += msg_count;

    if ((close_count = count_strinstr(buffer, "**CLOSE**")) > 0) {
        connection->channel_count += close_count;
        info("%d Channel(s) has(have) been closed by server.\n", close_count);
        if (connection->channel_count >= (connection->channel_end - connection->channel_start + 1)) {
            info("Connection %d will be closed \n", connection->index);
            close_connection(connection);
            stats->connections--;
        }
    }
}
void
read_response(Connection *connection, Statistics *stats, char *buffer, int buffer_len)
{
    int bytes_read = 0, bad_count = 0, ok_count = 0;

    bytes_read = read(connection->sd, buffer, buffer_len - 1);

    if (bytes_read < 0) {
        error("Error reading from socket for connection %d\n", connection->index);
        reopen_connection(connection);
        return;
    }

    if (bytes_read == 0) { // server disconnected us
        // reconnect
        info("Server disconnected as requested %d.\n", connection->index);
        close_connection(connection);
        return;
    }

    stats->bytes_read += bytes_read;
    buffer[bytes_read] = '\0';
    debug("Read %d bytes\n", bytes_read);
    trace("Read Message: %s\n", buffer);

    bad_count = count_strinstr(buffer, "HTTP/1.1 4");
    bad_count += count_strinstr(buffer, "HTTP/1.1 5");

    if (bad_count > 0) {
    	info("Recevied error. Buffer is %s\n", buffer);
        reopen_connection(connection);
        return;
    }

    ok_count = count_strinstr(buffer, "HTTP/1.1 200 OK");
    stats->messages += ok_count;
}