Not sure I fully understand the issue, but I'll give it a go.
To clarify, is the phone slowing everything down when it's trying to connect/disconnect? like, does it become less responsive when simply opening apps and typing? Or is it just the connection that is slow?
In either case, I'd try Settings > General > Reset > Reset Network Settings
- If the entire phone is slowing down when that happens, I'd have to assume a deeper software issue is going on. Consider Settings > General > Reset > Reset All Settings
If it's still acting up by that point, Restoring the device back to factory settings is the only thing I could reasonably assume would fix it, without knowing more information about the device.
- If it's just the connection that is slowing down, then it's something with the internet connection and not necessarily related to the phone itself. (which would make a lot more sense as to why a replacement would do the same thing)
If I misunderstood and it's actually just not connecting to Wi-Fi, then there is also this article: If your iPhone, iPad, or iPod touch won’t connect to a Wi-Fi network - Apple Support