Direct connection through NAT device

I have observed that if a device has not been online for a while, it is difficult for both parties to connect through NAT for the first time, but when both devices restart the program at the same time, there is a fairly high success rate to quickly establish a direct connection.