How to get server URL regardless where the code runs?

Hi, is there a way to get the server url and port number regardless whether the code is executed at the client or server?

application.getServerUrl() should already work pretty well in the latest 5.x or 6