The best way to avoid giving in to temptation is to flee from it the moment it appears. This is why Paul told his young friend Timothy to "Flee the evil desires of youth" (2 Timothy 2:22). The Bible also says, "Flee from sexual immorality" (1 Corinthians 6:18).
Did you ever play with a magnet when you were a child? If so, you may have noticed that the closer you brought it to a piece of iron, the stronger its pull became. The same is true of temptation. The longer you toy with it and think about it, the stronger its attraction becomes, and the more likely you are to give in to it. Flee temptation just as you would flee from a deadly poison, for that is what sin is–a deadly poison.
From what you say, however, you have already allowed this temptation to take root in your heart and mind. Don't tolerate it another minute, but do whatever you must do to flee from it. The Bible warns, "Can a man walk on hot coals without his feet being scorched? So is he who sleeps with another man's wife" (Proverbs 6:28-29).
Most of all, you need to yield yourself to Christ. God loves you and He doesn't want you to destroy your marriage and your future. Instead, He wants to help you resist temptation and live the way you should. Turn to Christ and commit your life to Him today.