I don't know if that code is the best way or not but it is certainly better than messing around with the modem/router.