checking connection inside loop

@arvenil, but if we have already opened connection, will it open second connection even if we use same variable for it?