This is a bit more tricky than it seems - socket API (I mean, Linux/Win32, not U++ encapsulation) is highly optimized and queued, for example Connect just starts connection to the port, without actually finishing it - and OS might detect that to connection is not available a couple of operations later.