Hello,
I would recommend you to make sure you implement a mechanism so that the client side keep the connection active, and attempt to reconnect if needed.
Error 521 occurs when the origin refuses the connection from the Cloudflare datacenters.
I would recommend you to make sure that all Cloudflare IPs are allowed on the origin side, and not rate-limited:
https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors