I added this extra check because otherwise the connection often failed without any apparent reason.