added better socket connection detection