Improve logic for building /api/sync URL from ws:// url

This does a better job figuring out a proper `http://` or `https://` protocol for the /api/sync URL based on `ws://` or `wss://`.  I've also improved the code that appends `/api/sync` to deal with an existing slash.