How should I defend against this sacrifice in the Two Knights Defense? I often get to play this opening :

[fen ""]
1. e4 e5 2. Nf3 Nc6 3. Bc4 Nf6 4. d3 d6 5. Nc3 Bg4 (5... Be7 6. Be3 Bg4) (4... Bc5 5. Nc3 d6 6. O-O Bg4)


Sometimes there may be other moves or developments before the pin, like in the variations. In any case, now that my light squared bishop is outside my pawn chain, it's hard for it to come back and defend my knight on f3. So I usually feel like I need to kick the bishop from g4 with a move like h3. After it retreats and 7.g4, I'll sometimes see my pawns captured :

[fen "r2qkb1r/ppp2ppp/2np1n2/4p3/2B1P1b1/2NP1N2/PPP2PPP/R1BQK2R w - - 0 6"]

6. h3 Bh5 7. g4 Nxg4 8. hxg4 Bxg4 (7... Bg6)


I looked at this position in Rybka and used the Chess.com computers to analyze the position, both of which label 7... Nxg4 as a mistake. However, I'm not sure how to punish this mistake, as afterwards, my knight is still pinned to the queen, and my bishop on c4 still can't find any good way to come back and defend.

Can someone explain to me the correct way to continue this position as white? I usually find myself losing the knight on f3, and eventually lose the position. Here is a link to the position in Chess.com's Game explorer. It isn't seen very often at master level play.
I almost always play 4. d4 instead of 4. d3, to keep that line open for the bishop, and open up the game a bit as well. That's not to say d3 isn't ok.

Without computer analysis:
That said, once they make the mistake of sacrificing that knight, you should quickly play Be3 to defend against them double attacking your pinned knight. Once that is accomplished, you can play Qe2 with the plan of castling queenside and evacuating your destroyed kingside pawn structure. Your goal should then be to carefully simplify by trading down until you get to an endgame where you're up a minor piece.



With computer analysis:
Stockfish recommends playing 9. Nd5, with the plan of playing 10. Ne3 for most responses, attacking the bishop on g4 (causing him to retreat with ...Bd7 or ...Be6, probably. The great thing to notice here is that 9... Nd4 is not actually a threat, even though at first glance it appears to lose the knight on f3. After 9... Nd4, White's tactic is as follows: 10. Nxd4! Bxd1 11. Bb5! Qd7 (there is no other option to prevent mate, either 11... Qd7 or 11... c6 12. Nxc6 bxc6 13. Bxc6 Qd7). Bad news for black. After the dust settles it probably looks something like this.



Back to the main line: 9. Nd5. Black can respond with a dozen reasonable moves (...Na5, ...Be7, ...h5, ...Qd7, ...Qc7, ...Qc8, ...a6, ...Rc8, ...a5, ...Rb8), all of which follow with 10. Ne3 (except for 9... Bxf3, in which case 10. Qxf3 is rather necessary). After this, you can start a kingside attack somewhat with Rg1, and getting your pieces out of the way for a castle queenside. You can turn your destroyed kingside pawns to your advantage by starting the attack there. Plenty of options from here on. You end up with something like:



For your second line, after you've already castled, I would not play h3 + g4, which weakens your king's defenses (even if they don't sacrifice a piece there). If you really want to play g4, you can still recover with Be3, defending against ...Nd4. They'll probably bring out the queen to attack (...Qf6), and you need to get your king out of there, undoing your castle to get your rook back outside of the king: Kg2 followed by Rg1 or Rh1. This situation isn't nearly as good for white; you'd be better off just avoiding g4 after castling, at least at this early stage.