fixed timing issue
more defensive connection management