[FIX] Proper port detection on templates

Fixes #2260