How do you want the "chat window" to behave? It looks like you're trying to create a @custom window that appears to have the same nick as someone's nick who has queried you, though even if the window appears to be named "TheirNick" it is actually named "@TheirNick".
If so, what you're doing would require intercepting all incoming queries, then checking if a @window already exists that's named similarly to the incoming nick. If not already existing, you'd need to create the @window. Then you'd need to echo the query message into the @window.
Doing something like this won't work for connecting to multiple networks, because you can't create the same window name @nick at 2 different networks at the same time.
If you're really wanting to just make the query messages be styled differently, then that can be done inside the existing query window.