S-duality in N = 4 supersymmetric gauge theories with arbitrary gauge group