ad_conn isconnected checks wether the running code is being called from within a connected thread or a scheduled procedure that runs without a connection. If you want to check if the user is logged in use something like auth::get_user_id - if it returns 0 then the user is not logged in.