Sometimes you will find your server is still online, pingable and all services are working. However, you can not connect via screen sharing. In this case you can restart or enable screen sharing via SSH.
It is possible to enable, restart and configure the built-in Screen Sharing/Remote Desktop functionality in macOS via command line (SSH). You can use the 'kickstart'-command for it.
You can find a few examples in the following support document from Apple: http://support.apple.com/kb/HT2370
Note for macOS 10.14+
For increased security in macOS 10.14 and later, Screen Sharing gives you view-only access when you enable Remote Management from a remote session. If you want to both view and control the remote Mac with Screen Sharing - you will need physical intervention from us to enable this on the device itself