DIAGNOSIS The printer's network interface is losing its IP address or its connection is being timed out by your router. This is often a DHCP or wireless band issue.
FIX On the printer control panel, press the Home button. Go to Network > Wireless > Settings. Write down the current IP address. Select 'Advanced Settings' > 'TCP/IP' > 'DHCP'. Set it to 'Off'. Manually configure the IP address using the one you wrote down, and set the gateway and subnet mask to match your network (usually 192.168.1.1 and 255.255.255.0). This gives it a static IP.
IF THAT DIDN'T WORK The 2.4GHz band is more stable for printers. Ensure your printer is connected to the 2.4GHz network, not the 5GHz. If your router combines them (band steering), temporarily disable that feature or create a separate 2.4GHz SSID for the printer.
HEADS UP Perform this setup close to your router. The M404dn has a basic antenna. Physical obstructions or distance will cause intermittent drops. A wired Ethernet connection is always the most reliable fix.