implemented state machine for managing reconnects