The Messenger of Allah, Prophet Muhammad, may Allah bless and grant him peace, was reported to have said:---"Allah has written for the son of Adam his inevitable share of adultery whether he is aware of it or not:The adultery of the eye is the looking (at something which is sinful to look at), and the adultery of the tongue is to utter ( what is unlawful to utter), and the inner self wishes and longs for (adultery) and the private parts that turn that into reality or refrain from submitting to the temptation". (Sahih Bukhari)
Immodesty, obscenity and any form of adultery be it of the eyes, ears, tongue or thoughts makes a hole in the heart. Through this hole leaks out Imaan/Faith. So, no matter how much you try to fill your heart with imaan, it will flow out because of this hole. While an inappropriate thought may occur to anybody, as Shaytan always tries to to inspire people with these, yet it is the person who decides whether to entertain such ideas, discussion, temptation or turn away from them.
If you are on a plateau, the more centrally the stand on it, the safer you are. The closer you get close to its edge/sides, the greater is the danger that you might fall off the edge. So, to avoid the major form of adultery, we must stay away from its minor forms....ie the adultery of the eyes, tongue, ears and thoughts. That way we will be safer. Avoid movies that glamorize the immoral, avoid listening to obscene talk, and if an indecent thought occurs to you, seek refuge with Allah Subhanahu Wa Ta'ala, from Shaytan and don't entertain such thoughts.