score:5
You will need an hour with the public transportation, a taxi will do it in 30-40 minutes. VOA line in IST is long sometimes (last time it took 30 minutes for me). Your arrival times (3:40pm-7:35 pm or 2:45pm-6:50pm) is in the rush hours and even if you obtain visa in time and run to the mosque right away it will be time for Asr prayer (around 5:20 pm these days) and the mosque will be closed for 90 minutes for prayers. The best time to visit the mosque is in the morning because the time between the first prayer (early morning) and second prayer (noon time) is quite long and gives you a better chance. You also will need another trip (1 hour?) to be back to the airport before the boarding time. I do not think you can do it with your current arrival times.
Here is a map for Istanbul public transportation just in case, you will ride starting from the airport (red line) and then change to the dark blue line to reach Sultan Ahmet area at the end of the line where the mosque is located.