Added locking to functions working with socket_nonblocking; better handling select() result