That error means you have some sort of network issue. It is best to connect the Mac directly to the router using an Ethernet cable for a faster more reliable connection which also bypasses any hard to troubleshoot WiFi issues. You can try power cycling the router making sure to wait at least five minutes for the router to finish booting. The issue could be with your router settings, or the issue may be with your ISP or local Internet (the latter items you have no control over).
If you have access to another compatible Mac, then you can try creating & using a bootable macOS USB installer using the instructions in this Apple article:
How to create a bootable installer for macOS - Apple Support
To know which Macs are compatible you will need to know the exact model of your iMac which you can find by entering the system serial number here:
Check Your Service and Support Coverage - Apple Support
Now you need to find another Mac which can also run an OS which your broken iMac can also run so you can download the appropriate macOS installer (the host Mac does not need to be currently running the OS you are downloading). Usually you only need to be concerned about the following operating systems: 10.11, 10.13, 10.15, 11.x since these are usually the last OS available for any series of Macs. You can use the following article to see which versions of macOS are compatible with various Apple hardware:
https://eshop.macsales.com/guides/Mac_OS_X_Compatibility