Although I don't have the time (or memory) to give you code, you should look into the whole non-blocking I/O beastiary. When you get down to it, non-blocking I/O is simply the ability to check for new data / write new data without having to wait for the operation to finish. That is exactly what you want.