Fixed wait_for_ssh to prevent invalid socket state