relay: fix indexes in CAP parsing and fix network on main thread when disconnecting