App freezes when a user has a very weak connection and I make a web call

I’m using the following code to check if the user has a connection

function networkCalls:isConnected() local socket = require("socket") local tcp = socket.tcp() tcp:settimeout(1, 't') -- Set timeout to 1 second local netConn = tcp:connect("www.google.com", 80) if(netConn == nil) then return false end tcp:close() return true end

The issue is the code works fine when the user has no connection like airplane mode. However, if the user has a weak connection like in the subway it passes the test but then fails and causes issues when I try to make a call to my server. Is there a fullproof way to check if the user has no connection or a very low / limited connection?