add counting of connections to avoid reconnect problem when callbacks are invoked in different order (fix #7)