Update connection.py

Use `is_connection` function instead of duplicating code.