You should read about TCP and splitting data stream into packets and learn how to understand packet header to assemble stream from packets.
Actually there are few different stages:
1. detect handshake to start new empty stream within your code;
2. detect subsequent packets and assemble stream (there are counters within packet header and they will help).
After creating start of TCP stream (usually 1KB is enough) you'll be able to detect is it HTTP request/header or no and start logging or ignoring packets for this connection.