I am in my 40's. My second marriage. My first ended because of multiple affairs. Ath that time I spent hour upon hour in Torah study. I attended the place of assembly reguglarly. I formed small groups of men for accountability, and yet I failed again and again.
Two things led to success: stark accountability and brokenness before HaShem's presence. Two things keep me safe now: full accountability to my wife, and the fear of losing again what I lost: my reputation, my children, employment, the respect of my sons and first wife, thousands of dollars of child support, etc.
I am now a shift supervisor in a retail store. I have working with me many young, attractive, and attentive females. There is always for me an initial attraction, but if I refuse to act out of that attraction, it subsides, and I realize it was just infatuation. My wife often asks me if I'm having "trouble" with anyone. I tell her. We pray, or she prays for me. (I do not feel anything then, it seems the confession is more empowering than the prayer, or so it seems)
I am working on not being so personal with the opposite sex. It is hard, I like to be involved in peoples' lives, male and female. I care when people are sad. I look out for the little guy. My wife warns me to maintain a professional relationship with my female coworkers.
The first thing to guard against it to avoid flattering compliments toward them. You may think it, but don't say it. The compulsion will pass, and you'll be glad you didn't say anything later.
A final note of encouragement: I have found that when I resist those I believe are available, that if I am strong enough, long enough, HaShem removes them from my workplace. They quit, move away, get fired. It's as if he says to me, "You've endured long enough! No more torture".
Then, another comes. But with each new coworker I become stronger and stronger.
I have to remember, I am not only standing for myself, but for Jews everywhere, even for Israel itself!
"Say to Wisdom, 'You are my sister!' and call Understanding, 'Friend!' that they may safeguard you from a forbidden woman, from a strange woman who makes her words glib" Proverbs 7:4,5
Berachot,
Tahor