Extract the method of waiting for a socket to be ready
Fix the behavior of waiting