fix the select() call to work after it has timed out, and always flush at the end...