I was able to fix my phone after fighting with it for a week. I found a video on YouTube (will post later when i find it again) of someone demonstrating what error 4013 meant when trying to update/restore the phone via iTunes. If that is the same error you are getting, which i presume you are since i was getting a boot loop with a green flash, there is a good chance it is due to the proximity sensor being damp/wet/foggy/etc.
Essentially what happened is i went to a water park, assuming my phone (XS Max) was water resistant up to 3 feet and 30 min, i went on a water slide while wrapping my phone in my shirt to keep it as dry as possible, however, it still did get wet, but not soaked and i dried it off immediately. The ride was only about 2 minutes long. Nevertheless, this apparently was not enough precaution for a water resistant phone.
Before you proceed to the fix below that worked for me, i would recommend trying to use a fan or something with forced air around the speaker/proximity sensor to try and potentially dry up any damp spots there. This "might" work but it did not work for me.
The fix for this was to take apart the phone, unplug the proximity sensor (after removing the metal plate that covers the connection) and try to start up the phone again (I highly recommend you google for a YouTube video on how to take the phone apart!). Since i was in restore mode, i decided to use iTunes to try and update my phone again with the proximity sensor unplugged. The phone updated successfully without displaying the 4013 error. It took about 30 min or so to update to a newer version of iOS. Afterwards, the phone went through a data retrieval process (about 30 more min), which was successful and then eventually turned on with no issues other than FaceID not working (which i assume was due to the proximity sensor not being plugged in). The next step was to take apart the proximity sensor. Be careful not to go too fast as some of the cords are glued down, but you can take apart the screen from the base of the phone pretty easily. First, unplug the batteries and then continue to take off the remaining metal cover and unplug the remaining two cords from the screen to take the screen off the base (assuming you have left the proximity sensor cord unplugged). Once you have done this, the proximity sensor has 3 screws. Once they are unscrewed, unfold the proximity sensor away from the top (do not try to pull it off as some of it is glued and still plugged in). Then use a fan or something that can provide forced air and use it on the proximity sensor for a good 30 seconds. This will hopefully help remove any dampness around it. Once you are done that, i would recommend plugging the 3 cords back to the base and plugging in the batteries. At this point, try to power the phone back on and hope that it starts without any issues. If it does, then put the phone back together and you should be good to go. If not, i would try to dry the proximity sensor again for another 30 seconds. Repeat these steps and hopefully at some point it will start working for you.
Hope this helps you guys as i know how frustrating this can be.
PS: I am not a repair specialist, however, i have personally repaired many phones myself so i would only recommend this to someone who has some technical experience and is not afraid that this could potentially result in a bricked phone. Also, keep in mind that once you take off the screen, any water resistance the phone had, is now gone since the sealant around the screen is now broken. You have two options here, leave it as is or purchase a new sealant that you can put around the screen to make it water resistant again.